0

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);
Community
  • 1
  • 1
DolDurma
  • 15,753
  • 51
  • 198
  • 377

1 Answers1

5

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);
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • 1
    you dont *save* `savedInstanceState` anywhere, `savedInstanceState` is an object that holds the data you want to save, which is re-accessed in `onCreate()`. You need to create a class `MyClass` that holds the boolean and integer you want to save and implement the `Parcelable` interface on this class. Then save an instance of this class in the `savedInstanceState` object. See edited answer – Yash Sampat Mar 03 '15 at 09:32