The status of the android recovery instance for the fragment that is in the back

Hi I have an activity that has two fragments.

  • Fragment A
  • Fragment B

    • Slice A has an EditText and ListView.
    • As soon as I type something into the EditText and press Enter , I populate the ListView.
    • Now the ListView fragment is filled with data.
    • Clicking on any item in ListView of fragment A will send fragment B to the user.
    • At this point I am replacing fragment A with fragment B
    • Therefore, when the user accesses the button, he returns to fragment A

Now the problem is that the user is in fragment B, and configuration changes occur, for example, when the screen is rotated, etc., then my ListView is empty, because my arraylist is null.

Please note that I use onSavedInstanceState in fragment A and fragment B. If my current fragment is fragment A and configuration changes, then there is no problem with restoring the state, because in onCreateView I get arraylist from the package.

I know the reason my arraylist is null when I return from fragment B to fragment A, which was previously in the backstack. When fragment A is in backstack state, the only method that is called is onSaveInstanceState, so after the first configuration change, my arraylist field is null, because I could not assign my arraylist stored in the savedInstateState package to the arraylist field.

I do not want to use android: configchanges in my manifest.

My question is how to restore the state of the fragment that is in the backstack.

+5
source share
3 answers

I have achieved the goal of preserving the state of the fragment that is in the backstack, passing all the details of this fragment of the main activity before it is added to the backstack, and preserving the state in the main state of saveInstance and whenever the backstack and the previous fragment are visible, I pass the saved state of the instance of this fragment to it from the main action, and in this way I save the state. I am not sure if this is the right approach.

+2
source

I reviewed your scenario. Basically, you do the right job when you add an arraylist to the savedinstanceState of your fragment, but, in my opinion, you make a small mistake in your activity.

As when changing the orientation of an Activity call onDestroy then onCreate in this scenario, if you do not check that either your fragment is already added or not, it will replace it with a new one. Here is the code you should put in onCreate ()

FragmentManager fragmentManager = getFragmentManager(); Fragment fragment = fragmentManager.findFragmentByTag("FragmentA"); if(fragment==null){ fragmentManager.beginTransaction().add(R.id.container, new FrgamentA(), "FragmentA").commit(); } 

Another point is that you want your first fragment to be saved, add

 setRetainInstance(true); 

in onCreate a fragment.

Please find this demo on GitHub inpulse-fragment

In this demo, on the first fragment, when you increase the counter, it will appear in the text form above, and if you click on this text representation, you will go to fragment B. Now you can try to rotate. Let me know in case of any inquiries.

+2
source

You can use setRetaininstance(true) in onAttach , and this line is ConfigChanges = "screenSize|keyBoardHidden|orientation" in the activity manifest file.

0
source

All Articles