开发者

How to pass object between activities in android [duplicate]

This question already has answers here: 开发者_StackOverflow社区 Closed 11 years ago.

Possible Duplicate:

How to pass object from one activity to another in Android

While retrieving appSession I get a RunTimeException:

appSession = (ApplicationSession)intent.getParcelableExtra("appSession");

I am creating a app in which at the launch of app I create an ApplicationSession class object. I want to pass this object to all activities upon launch. How do I achieve this?

// app start
// contains data specific to app which I need to use across all activites.
ApplicationSession appSession = new ApplicationSession(); 

How to pass appSession to all activites?


Make ApplicationSession implement Parcelable, and when you are starting an Activity try something like:

ApplicationSession appSession = new ApplicationSession();
Intent i = new Intent(context, YourActivityName.class);
i.putExtra("appSession", appSession);
startActivity(i);

OR if it makes sense in your use case, just make ApplicationSession a static singleton class and let it live in a subclass of Application that you write.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜