我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
第一种方式:在当前“活动”中,当您创建一个意图打开新屏幕的对象时:
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类使用序列化在类之间传递数据。
其他回答
活动之间的数据传递主要通过意向对象进行。
首先,必须使用Bundle类将数据附加到intent对象。然后使用startActivity()或startActivityForResult()方法调用活动。
您可以从博客文章“将数据传递给活动”中找到更多信息。
Intent intent = new Intent(YourCurrentActivity.this, YourActivityName.class);
intent.putExtra("NAme","John");
intent.putExtra("Id",1);
startActivity(intent);
您可以在其他活动中检索它。两种方式:
int id = getIntent.getIntExtra("id", /* defaltvalue */ 2);
第二种方法是:
Intent i = getIntent();
String name = i.getStringExtra("name");
查理·柯林斯用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();
}
}
}
第一种方式:在当前“活动”中,当您创建一个意图打开新屏幕的对象时:
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类使用序列化在类之间传递数据。
使用全局类:
public class GlobalClass extends Application
{
private float vitamin_a;
public float getVitaminA() {
return vitamin_a;
}
public void setVitaminA(float vitamin_a) {
this.vitamin_a = vitamin_a;
}
}
您可以从所有其他类调用该类的setter和getter。要做到这一点,您需要在每个活动中创建一个GlobalClass对象:
GlobalClass gc = (GlobalClass) getApplication();
然后,您可以调用例如:
gc.getVitaminA()