我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
查理·柯林斯用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();
}
}
}
其他回答
数据对象应扩展Parcelable或Serializable类。
Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
mIntent.putExtra("data", data);
startActivity(mIntent);
我最近发布了Vapor API,这是一个jQuery风格的Android框架,它使类似这样的所有任务都更加简单。如前所述,SharedPreferences是实现此目的的一种方法。
VaporSharedPreferences实现为Singleton,因此这是一个选项,在VaporAPI中,它有一个重载的.put(…)方法,因此您不必明确担心您提交的数据类型-前提是它受支持。它也很流畅,因此您可以拨打电话:
$.prefs(...).put("val1", 123).put("val2", "Hello World!").put("something", 3.34);
它还可以选择自动保存更改,并在后台统一读写过程,因此您不需要像在标准Android中那样显式检索编辑器。
或者,您可以使用意向。在VaporAPI中,您还可以对VaporContent使用可链接的overload.put(…)方法:
$.Intent().put("data", "myData").put("more", 568)...
如其他答案中所述,将其作为附加项传递。您可以从“活动”中检索额外内容,此外,如果您正在使用VaporActivity,这将自动为您完成,因此您可以使用:
this.extras()
要在切换到的“活动”的另一端检索它们。
希望有人对此感兴趣:)
第一种方式:在当前“活动”中,当您创建一个意图打开新屏幕的对象时:
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类使用序列化在类之间传递数据。
在当前活动中创建新的意向。
String myData="Your string/data here";
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("your_key",myData);
startActivity(intent);
在SecondActivity.java onCreate()中使用键your_key检索这些值
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String myData = extras.getString("your_key");
}
}
在当前活动中,创建一个新的意向:
String value="Hello world";
Intent i = new Intent(CurrentActivity.this, NewActivity.class);
i.putExtra("key",value);
startActivity(i);
然后在新的“活动”中,检索这些值:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
//The key argument here must match that used in the other activity
}
使用此技术将变量从一个Activity传递到另一个Activity。