Dynamically delete fragments and save their user interfaces

I have a couple of fragments that replace each other. The user interface of these fragments is changing, and I need to save it in a new state. So the code looks pretty trivial:

FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); if (mStepTwo == null) { mStepTwo = new QuizStepTwo(); mStepTwo.setListener(mStepTwoListener); } else { fragmentTransaction.remove(mStepTwo); } fragmentTransaction.replace(R.id.step_holder, mStepTwo); fragmentTransaction.addToBackStack("second_step"); fragmentTransaction.commit(); 

However, when I replace the second step with the first, for example, by pressing the back button, its โ€œuser interface stateโ€ returns to the original one.

How to save a state? OnSaveInstanceState? or something more comfortable?

Related questions: Android fragment view state in the tab node , How to restore Android fragment view state

+6
source share
5 answers

I have not used this, so the mileage may vary. You can try using the FragmentManager.saveFragmentInstanceState method when replacing fragments, then use Fragment.setInitialSavedState when restoring a fragment. From the documents, it sounds as if it can only work when creating a new fragment of the same type, so you are not sure if this will work with your current implementation.

+3
source

This question (and its solution) is similar to what you are trying to do.

However, one of the alternative solutions that I can think of would not be to replace the first fragment with the second, but rather hide it (using fragmentTransaction.hide() ) or, possibly, disconnect. Thus, when the user presses BACK , the fragment will be shown and its state will be intact. This may take more memory, but it should still work as the fragment will not be destroyed.

+2
source

If the state depends on your application data, it is best to save them in a SharedPreferences file and use it to create user interface elements.

+1
source

Take a look at the sample documentation for the snippet :

You can use the onSaveInstanceState method to store your state

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

And then fetch it in the onActivityCreated callback

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

The difference between adding and replacing is that adding the previous fragment does not pause or stop. where, when we try to replace a fragment, the previous one goes through a pause cycle and stops. but when we push it onto the stack, the previous fragment is saved on top of the stack. And retrieved and loaded into events such as back-press.

So, if your solution requires your user interface to never stop or unload, you should just hide it and add a fragment on top of it. Or if its OK to unload the fragment and the user interface can be regenerated using some parameters. Thus, you can have some parameter in the fragment memory itself, which determines what statistics it should adhere to. By default, it will try to save a specific default state. But you can create your own object and attach it to the fragment.

As with onSaveInstanceState, it will be called when the action containing the fragment is closed. Events such as home tapping.

eg

 public class MainActivityFragment1 extends Fragment { int alreadyCreated = 0; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (alreadyCreated == 1) { // restoreLayout(); Log.i("MainActivity", " Fragment already created and can be restored Fragment 1"); }else Log.i("MainActivity", " Running Once Fragment 1"); alreadyCreated = 1; 01-07 16:08:07.009: I/MainActivity(14722): Running Once Fragment 1 01-07 16:08:09.239: I/MainActivity(14722): MainActivity I on pause 01-07 16:08:09.239: I/MainActivity(14722): MainActivity I on stop 01-07 16:08:09.239: I/MainActivity(14722): Running Once Fragment 2 01-07 16:08:13.499: I/MainActivity(14722): Back Pressed 01-07 16:08:13.619: I/MainActivity(14722): MainActivity II on pause 01-07 16:08:13.619: I/MainActivity(14722): MainActivity II on stop 01-07 16:08:13.619: I/MainActivity(14722): MainActivity II on stop 01-07 16:08:13.619: I/MainActivity(14722): MainActivity II on stop 01-07 16:08:13.619: I/MainActivity(14722): Fragment already created and can be restored Fragment 1 
0
source

All Articles