可能重复:
How to pass object from one activity to another in Android
在检索appSession时,我收到RunTimeException:
appSession = (ApplicationSession)intent.getParcelableExtra("appSession");
我正在创建一个应用程序,在应用程序启动时我创建了一个ApplicationSession类对象。我希望在启动时将此对象传递给所有活动。我如何实现这一目标?
// app start
// contains data specific to app which I need to use across all activites.
ApplicationSession appSession = new ApplicationSession();
如何将appSession
传递给所有活动?
答案 0 :(得分:2)
制作ApplicationSession
implement
Parcelable
,当您开始Activity
时尝试以下内容:
ApplicationSession appSession = new ApplicationSession();
Intent i = new Intent(context, YourActivityName.class);
i.putExtra("appSession", appSession);
startActivity(i);
如果在您的用例中有意义,只需将ApplicationSession
设为静态singleton类,并让它存在于您编写的Application
的子类中。