DolDurma
DolDurma

Reputation: 17331

Android save Activity state in Parcelable class object

in my application i many onSaveInstanceState in each activity and file, now after reading this link to create Parcelable class i can not use that and save into this class

default onSaveInstanceState is:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putBoolean("IsShowingExitDialogs", mIsShowingExitDialogs);
    savedInstanceState.getInt("LastMenuItemSelected", mLastMenuItemSelected);
}

now i can not use this below line to save IsShowingExitDialogs or LastMenuItemSelected to Parcable class:

savedInstanceState.putParcelable("IsShowingExitDialogs", mIsShowingExitDialogs);
savedInstanceState.putParcelable("IsShowingExitDialogs", mLastMenuItemSelected);

Upvotes: 0

Views: 3124

Answers (1)

Yash Sampat
Yash Sampat

Reputation: 30611

Parcelable is an interface applied on classes. It works for objects, whereas in your case you are trying to save an integer and a boolean, both of which are primitive types. If you really want to do this, you need to wrap them inside a class that implements Parcelable. Then this will work.

EDIT:

1. The savedInstanceState that you save in onSavedInstanceState() is returned in onCreate() when the Activity is recreated, allowing you to reuse the data that you saved previously.

2. You need to create a custom class MyClass extends Parcelable, implement the interface methods and save it like this:

MyClass myClass = new MyClass(mIsShowingExitDialogs, mLastMenuItemSelected);
savedInstanceState.putParcelable("myClass", myClass);

Upvotes: 5

Related Questions