129

Am I missing something or do Fragments not have a onRestoreInstanceState() method? If not, how do I go about attaining something similar?

biegleux
  • 12,934
  • 11
  • 42
  • 52
Shaun
  • 5,293
  • 10
  • 37
  • 48

5 Answers5

211

Fragments do not have an onRestoreInstanceState method.

You can achieve the same result in onActivityCreated, which receives a bundle with the saved instance state (or null).

Check the source code here.

mgv
  • 8,046
  • 3
  • 40
  • 46
  • 92
    That's not a consistent design, is it? – Ehtesh Choudhury Jan 27 '12 at 18:31
  • 5
    This also doesn't take care of resuming the fragment does it? its fine for configuration changes, but what if you go to an activity from a fragment and then go back to the fragment? – speedynomads Jun 04 '13 at 16:41
  • 1
    Fragments should belong to an activity to begin with, so if you save in the fragment, it saves to the parent activity. So if you leave that fragment (which belongs to an activity) and go back to the fragment, all values are then restored from the parent activity – Shaun Dec 05 '13 at 03:16
  • 3
    onActivityCreated() is not the only method to restore the fragment's state.You can restore it during either onCreate(), onCreateView(), or onActivityCreated(). http://developer.android.com/guide/components/fragments.html – shaby Mar 21 '16 at 16:56
  • 4
    @EhteshChoudhury Nothing in Android app development is "consistent design". It's all messed up and everything is "deprecated". – Karanveer Singh Feb 09 '18 at 07:22
  • 1
    `onActivityCreated` is now deprecated in class `Fragment`. See this [post](https://stackoverflow.com/a/62076948/3014115) on how to deal with that. – Tjaart Jun 24 '20 at 22:24
47

I know, that you have accepted answer, but you should read the official documentation about fragments, and it says (paragraph "Handling the Fragment Lifecycle"):

You can retain the state of a fragment using a Bundle, in case the activity's process is killed and you need to restore the fragment state when the activity is recreated. You can save the state during the fragment's onSaveInstanceState() callback and restore it during either onCreate(), onCreateView(), or onActivityCreated()

So, you can use that suits you best: onCreate(), onCreateView(), or onActivityCreated()

jimpanzer
  • 3,356
  • 3
  • 39
  • 84
30

In Fragments guide's ListFragment example you can find:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("curChoice", mCurCheckPosition);
}

Which you can use like this:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        // Restore last state for checked position.
        mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
    }
}

onActivityCreated() is invoked after the fragment returns back from the stack.

Ravi K Thapliyal
  • 46,997
  • 8
  • 70
  • 84
Gaurav Darji
  • 488
  • 5
  • 12
17

onViewStateRestored of Fragment is the equivalent of onRestoreInstanceState of Activity. But it is called after onActivityCreated(Bundle) and before onStart().

nhkhanh
  • 1,471
  • 13
  • 16
1

onActivityCreated is deprecated. and i found it just confusing in terms of the fragment lifecycle. just do this:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("curChoice", mCurCheckPosition);
}

// and then:

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    savedIInistanceState?.let{ 
//restore the data here
    }
    }
j2emanue
  • 51,417
  • 46
  • 239
  • 380