1

I have a Fragment F attached on Activity A. When another activity becomes front-activity it is called onSaveInstanceState. I've overrided that function and it looks like:

public void onSaveInstanceState(Bundle outState){
 saveState_to_outstate
}

Now, when the Fragment F is front again it is called onViewCreated(View view, Bundle savedInstanceState). I would like to restore previously saved state but I cannot because savedInstanceState is null though it was written before in onSaveInstanceState.

Why it happened?

petrumo
  • 1,116
  • 9
  • 18
  • please edit your question and add the code where you are "attaching" Fragment F to Activity A – petey Jan 04 '17 at 19:24
  • Your code snippet doesn't do anything .. specifically `saveState_to_outstate` does nothing, you need to use the `outState` `Bundle` from the method argument to save data. – Mark Jan 04 '17 at 19:44

2 Answers2

0

If you want to restore Fragment F with previous Data. As according to my knowledge you should perform inside onViewStateRestored() method of Fragment.

OnViewStateRestored (Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored. you Should visit this link for your reference:Fragment

K P
  • 182
  • 9
0

Method A: You can pass the data from Activity A to Fragment F by bundle or intent. it's similar to usage of saving instance. Send data from activity to fragment in android

MethodB: You can only use following function with onCreate() at the same place, either fragment F or Activity A.

public void onSaveInstanceState(Bundle outState){ outState.putInt("STATE_NAME", 123);//123 is the data you gonna save super.onSaveInstanceState(outState);//Don't forget to put this }

MethodC: Use hardcode-like SharePreference Android Shared preferences example ,it's also like the bundle and intent, and it won't be effected by Activity or fragment life periods.

Community
  • 1
  • 1