2

I am trying to explore about how fragments are managed by android framework and through my research I got to know so many new things that I didn't know about fragments but I got stuck at one point and can't figure out how this is happening.

Please try to understand my scenario first. It goes like that: I have one Activity which add two fragments one by one. When activity is first loaded then Fragment A is attached to it using below code:

private void initFirstFragment(){
    Bundle bundle = new Bundle();
    bundle.putString("TEXT_TO_SHOW", "FIRST ACTIVITY\nFIRST DUMMY FRAGMENT");
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.frameLayoutFragmentContainer, FirstDummyFragment.newInstance(bundle), FirstDummyFragment.class.getSimpleName());
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

These callback methods of Fragment A is called when it is loaded

FirstDummyFragment: onCreate: savedInstanceState--->null

FirstDummyFragment: onCreateView: savedInstanceState--->null

FirstDummyFragment: onResume

Now in Fragment A, I have a edit text and I type some text into it.

When a button is clicked inside an Activity then Fragment B is added to same container using below code:

public void openSecondFragment() {
    Bundle bundle = new Bundle();
    bundle.putString("TEXT_TO_SHOW", "FIRST ACTIVITY\nSECOND DUMMY FRAGMENT");
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.frameLayoutFragmentContainer, SecondDummyFragment.newInstance(bundle), SecondDummyFragment.class.getSimpleName());
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

Below callback methods are called after adding Fragment B

SecondDummyFragment: onCreate: savedInstanceState--->null

FirstDummyFragment: onDestroyView

SecondDummyFragment: onCreateView

SecondDummyFragment: onResume

When I press back button, Fragment B is destroyed and Fragment A comes to foreground and below callback methods are called

SecondDummyFragment: onDestroyView

SecondDummyFragment: onDestroy

SecondDummyFragment: onDetach

FirstDummyFragment: onCreateView: savedInstanceState--->null

FirstDummyFragment: onResume

And the edit text of fragment A contains same text that I entered into it earlier before adding Fragment B. I am confused how android is restoring Fragment A's view state even if savedInstanceState is null and onCreateView returns a whole new View object when Fragment A is created again.

Community
  • 1
  • 1
Vikalp
  • 2,051
  • 4
  • 18
  • 24
  • This should help you. https://stackoverflow.com/questions/15313598/once-for-all-how-to-correctly-save-instance-state-of-fragments-in-back-stack – ashkhn Jan 09 '17 at 01:34
  • @akash93 It does not relate to my question. – Vikalp Jan 09 '17 at 07:55

1 Answers1

1

Finally I found my answer here.

Android is designed this way. View State Saving/Restoring are internally called inside Fragment in this case. As a result, every single View that is implemented a View State Saving/Restoring internally, for example EditText or TextView with android:freezeText="true", will be automatically saved and restored the state. Causes it to display just perfectly the same as previous.

Vikalp
  • 2,051
  • 4
  • 18
  • 24
  • I think this is true and yet not very well understood by many Android developers. The methods surrounding instance state only seem to refer to the internal state of the fragment (i.e. the fragment's instance variables), which is supposed to be saved in and restored from the `savedInstanceState` bundle. But this only happens when the *parent activity* saves its state (i.e. on a configuration change, such as putting on an activity backstack or on device rotation). When a fragment is backstacked, the instance (including its state) is kept with only the view that needs to be restored. – bompf Nov 20 '18 at 12:37