j2emanue
j2emanue

Reputation: 62519

android - manually calling onSaveInstanceState not working

I have a android fragment hosted in an activity. The activity is not overriding any onsaveInstanceState or onRestoreInstanceState methods. in my fragment i put the following code:

  @Override
    public void onDestroy() {
        super.onDestroy();
        Bundle b = new Bundle();
        b.putParcelable("mymodel", myModel);
        onSaveInstanceState(b);
    }

when user hits the back button i see this being called (i know its not guaranteed for ondestroy to be called, thats not my point). so i put a break point and indeed this code gets called. Then i return to the activity and it opens the fragment but the savedInstanceState is null in oncreate and in onViewCreated. What am i doing wrong ? i thought i could call this manually to save state ?

Upvotes: 0

Views: 960

Answers (2)

Adib Faramarzi
Adib Faramarzi

Reputation: 4064

You should not call onSaveInstanceState manually, it is Android Framework's job to do that whenever it is necessary.

If you want to save information and retrieve it later, just override onSaveInstanceState and put your info in the bundle.

Upvotes: 1

tompee
tompee

Reputation: 1408

No you cannot call onSaveInstanceState manually. The framework does that for you. Calling that will only update your bundle, not the framework bundle. The proper way is to override it and save your data.

Upvotes: 1

Related Questions