I have ActivityA and ActivityB. ActivityB is called from ActivityA.
I wanted to retain the object of ActivityA, when I press back button of ActionBar or back button of device.
I have created a Singleton object to save my object and to get my object.
onPause() I am saving the object and onResume() method I am getting the object.
My problem here is the behaviour is different, the object that I get when back button of Action (in ActivityB) is different from the object that I get when back button of back button is clicked.
Here is the code:
protected void onPause() {
//resetSort();
super.onPause();
MySingleton.getInstance().setMyObject(myObject);
}
@Override
protected void onResume() {
super.onResume();
myObject = MySingleton.getInstance().getMyObject();
}
Here is my manifest file.
<activity
android:name=".ActivityA"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
>
</activity>
<activity
android:name=".ActivityB"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name" >
</activity>
Code from ActivityB:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Any solution is appreciated. Thanks
The most reliable way is to use SharedPreferences . It's both thread safe and remains even if your app is killed off.
Google's Storage Options guide is also definitely worth a look if you haven't had a chance to review it yet.
To save a String you would use:
String str = "abc";
getSharedPreferences("filename", Context.MODE_PRIVATE).edit().putString("String Name", str).commit();
And then to retrieve it:
String str = getSharedPreferences("filename", Context.MODE_PRIVATE).getString("String Name", "default string")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.