我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
换句话说,您可以使用接口传递数据。
我们有两个活动A,B,那么我该怎么做,创建一个界面,如:
public interface M{
void data(String m);
}
然后,您可以调用赋值给这个方法,如下面A类中的代码所示:
public class A extends AppCompatActivity{
M m; //inteface name
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
m= (M) getActivity();
//now call method in interface and send data im sending direct you can use same on click
m.data("Rajeev");
}
}
现在您必须在类B中实现该接口:
public class B extends AppCompatActivity implements M{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
}
@Override
public void data(String m) {
you can use m as your data to toast the value here it will be same value what you sent from class A
}
}
其他回答
最简单的方法是将会话id传递给用于启动活动的Intent中的注销活动:
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);
访问下一个活动的意图:
String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");
Intents的文档有更多信息(请参阅标题为“附加”的部分)。
查理·柯林斯用Application.class给了我一个完美的答案。我不知道我们可以这么容易地将其子类化。这里是一个使用自定义应用程序类的简化示例。
AndroidManifest.xml
赋予android:name属性以使用您自己的应用程序类。
...
<application android:name="MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
....
我的应用程序.java
将其用作全局引用持有者。它在同一个过程中运行良好。
public class MyApplication extends Application {
private MainActivity mainActivity;
@Override
public void onCreate() {
super.onCreate();
}
public void setMainActivity(MainActivity activity) { this.mainActivity=activity; }
public MainActivity getMainActivity() { return mainActivity; }
}
主要活动.java
设置应用程序实例的全局“singleton”引用。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((MyApplication)getApplication()).setMainActivity(this);
}
...
}
我的首选项.java
一个简单的例子,我使用另一个活动实例的主活动。
public class MyPreferences extends PreferenceActivity
implements SharedPreferences.OnSharedPreferenceChangeListener {
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (!key.equals("autostart")) {
((MyApplication)getApplication()).getMainActivity().refreshUI();
}
}
}
使用Intent将数据传递到一个Activity to AnotheActivity的最佳方法,
检查剪下的代码
活动One.java
Intent myIntent = new Intent(this, NewActivity.class);
myIntent.putExtra("key_name_one", "Your Data value here");
myIntent.putExtra("key_name_two", "Your data value here");
startActivity(myIntent)
第二次活动
第二活动.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Intent intent = getIntent();
String valueOne = intent.getStringExtra("key_name_one");
String valueTwo = intent.getStringExtra("key_name_two");
}
第一种方式:在当前“活动”中,当您创建一个意图打开新屏幕的对象时:
String value="xyz";
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("key", value);
startActivity(intent);
然后在onCreate方法的nextActivity中,检索从上一个活动传递的值:
if (getIntent().getExtras() != null) {
String value = getIntent().getStringExtra("key");
//The key argument must always match that used send and retrieve value from one activity to another.
}
第二种方法:您可以创建一个bundle对象并将值放入bundle中,然后将bundle对象放入当前活动的意图中-
String value="xyz";
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", value);
intent.putExtra("bundle_key", bundle);
startActivity(intent);
然后在onCreate方法的nextActivity中,检索从上一个活动传递的值:
if (getIntent().getExtras() != null) {
Bundle bundle = getIntent().getStringExtra("bundle_key");
String value = bundle.getString("key");
//The key argument must always match that used send and retrieve value from one activity to another.
}
您还可以使用bean类使用序列化在类之间传递数据。
更新注意,我已经提到了SharedPreference的使用。它有一个简单的API,可以跨应用程序的活动访问。但这是一个笨拙的解决方案,如果您传递敏感数据,则会带来安全风险。最好使用意图。它包含大量重载方法,可用于在活动之间更好地传输许多不同的数据类型。看看intent.putExtra。这个链接很好地展示了putExtra的用法。
在活动之间传递数据时,我的首选方法是为相关活动创建一个静态方法,其中包含启动意图所需的参数。这样就可以方便地设置和检索参数。所以它看起来像这样
public class MyActivity extends Activity {
public static final String ARG_PARAM1 = "arg_param1";
...
public static getIntent(Activity from, String param1, Long param2...) {
Intent intent = new Intent(from, MyActivity.class);
intent.putExtra(ARG_PARAM1, param1);
intent.putExtra(ARG_PARAM2, param2);
return intent;
}
....
// Use it like this.
startActivity(MyActvitiy.getIntent(FromActivity.this, varA, varB, ...));
...
然后,您可以为预期活动创建意图,并确保您拥有所有参数。你可以将碎片调整为。上面的一个简单示例,但你明白了。