Search code examples
javaandroidstaterestore

Is this possible to restore the state of data in UI of fragment while calling fragment again?


If I have Fragment A and Fragment B. I call Fragment B from A using below code

FragmentManager fragmentManager =getFragmentManager();
    FragmentTransaction fragmentTransaction =
            fragmentManager.beginTransaction();


    fragmentTransaction.add(R.id.frame_step, fragment,TAG).hide(SourceFragment.this).addToBackStack((SourceFragment.class.getName()));
    fragmentTransaction.commit();

Now in Fragment B I have EditText & I have entered "Hello" in it, if I press back button from Fragment B then according to getSupportFragmentManager().popBackStack(); it will resume Fragment A

Now if I again call Fragment B from Fragment A I want that FragmentB will not get created again and still I can see "Hello" inside EditText.

Note -- I do not want to use variables or shared pref for this as i have multiple fragments with multiple views like a big form. Is there anything that can just call fragment from its resume state instead of calling it again or if i can check if this fragment has already created . Thanks in advance


Solution

  • What you want is possible. You need to save fragment state before forward navigation and restore it after fragment is restored from backstack.

    In Fragment

    private Bundle savedState = null;
    
     @Override
        public void onDestroyView() {
            super.onDestroyView();
            savedState = saveState();
        }
    
        private Bundle saveState() { /* called either from onDestroyView() or onSaveInstanceState() */
            Bundle state = new Bundle();
            state.putCharSequence("TEXT_HELLO_WORD", helloWordTextView.getText());
            return state;
        }
    
     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            /* (...) */
    
            if(savedInstanceState != null && savedState == null) {
                savedState = savedInstanceState.getBundle("FRAGMENT_HELLO_WORD");
            }
            if(savedState != null) {
             helloWordTextView.setText(savedState.getCharSequence("TEXT_HELLO_WORD"));
            }
            savedState = null;
    
            /* (...) */
            return view;
        }
    ...
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    
        //Save the fragment's state here somehow like this
        outState.putBundle("FRAGMENT_HELLO_WORD", (savedState != null) ? savedState : saveState());
    }
    

    in Activity

    public void onCreate(Bundle savedInstanceState) {
        ...
        if (savedInstanceState != null) {
            //Restore the fragment's instance
            mContent = getSupportFragmentManager().getFragment(savedInstanceState, "myFragmentName");
            ...
        }
        ...
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    
        //Save the fragment's instance
        getSupportFragmentManager().putFragment(outState, "myFragmentName", mContent);
    }
    

    Answer compiled from here and here

    Hope it helps.