2

I have a series of fragments. And I use "previous" and "next" buttons for navigation within this fragments. There are many edit texts and radio buttons in this fragments.

I want to save and and restore the user input in these edit texts and radio buttons when a previous fragment is loaded by clicking on "previous" button.

Screeshots:
Fragment 1

Fragment 2

Fragment 1:

public class Register_Page6 extends Fragment {
public Register_Page6() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_register_page6, container, false);

    Button Previous = (Button) view.findViewById(R.id.Previous6);
    Button Next = (Button) view.findViewById(R.id.Next6);
    Next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            android.support.v4.app.FragmentTransaction FT;
            FT = getActivity().getSupportFragmentManager().beginTransaction();
            FT.replace(R.id.main_container,new Register_Page7());
            FT.commit();
        }
    });

    Previous.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            android.support.v4.app.FragmentTransaction FT;
            FT = getActivity().getSupportFragmentManager().beginTransaction();
            FT.replace(R.id.main_container,new Register_Page5());
            FT.commit();
        }
    });
    return view;
}
}

Fragment 2:

public class Register_Page7 extends Fragment {
public Register_Page7(){

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_register_page7, container, false);

    Button Previous = (Button) view.findViewById(R.id.Previous7);
    Button Regiter = (Button) view.findViewById(R.id.Submit);
    Regiter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    android.support.v4.app.FragmentTransaction FT;
            FT = getActivity().getSupportFragmentManager().beginTransaction();
            FT.replace(R.id.main_container,new Register_Page6());
            FT.commit();
        }
    });

    Previous.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            android.support.v4.app.FragmentTransaction FT;
            FT = getActivity().getSupportFragmentManager().beginTransaction();
            FT.replace(R.id.main_container,new Register_Page6());
            FT.commit();
        }
    });
    return view;
}
}
Tony Mathew
  • 191
  • 2
  • 12
  • Check [My answer here](http://stackoverflow.com/questions/39607821/app-crash-when-i-change-the-device-orientation/39607903#39607903) – Piyush Sep 27 '16 at 09:54
  • @Piyush : I shouldn't add this : "android:configChanges="orientation|screenSize|keyboardHideen" to my code, right? – Tony Mathew Sep 27 '16 at 10:00
  • If you want to store your data (fragment state) with orientation change then u need to add in your manifest file for activity. – Piyush Sep 27 '16 at 10:02
  • @Piyush: I dont have an oncreate method in my fragment. Should I use "setRetainInstance(true);" in onCreateView instead? – Tony Mathew Sep 27 '16 at 10:06

2 Answers2

3

Add Your fragment to back stack using addToBackStack("YOUTAG") like this in all your fragments :

android.support.v4.app.FragmentTransaction FT;
FT = getActivity().getSupportFragmentManager().beginTransaction();
FT.replace(R.id.main_container, new Register_Page6()).addToBackStack("FragmentName");
FT.commit();
Ziem
  • 6,579
  • 8
  • 53
  • 86
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
  • Thats it? Just add the fragments to back stack? Will this restore the fragment state when I click "Previous" button? – Tony Mathew Sep 27 '16 at 09:58
  • Yeah..it will store the values..when you click previous you need to `popback()` stack manually to get previous fragment with all its state – Burhanuddin Rashid Sep 27 '16 at 10:02
  • Im confused. Can you please tell me what exactly should i do when I click Next and Previos buttons? – Tony Mathew Sep 27 '16 at 10:04
  • Replace fragment with adding to backstack when you click next as i meantion in the anser and when you click previous execute this code `getActivity().getSupportFragmentManager().popBackStack()` on previous button clicklistner – Burhanuddin Rashid Sep 27 '16 at 10:06
  • After trying your code, when I click on the physical back button, the fragment state gets restored but not when I click on precious button. I did include "getActivity().getSupportFragmentManager().popBackStack()" in the "previous" button click event – Tony Mathew Sep 27 '16 at 10:22
  • remove fragemnt replace code from previous button click listner and just put this code only `getActivity().getSupportFragmentManager().popBackStack()` – Burhanuddin Rashid Sep 27 '16 at 10:27
  • That worked! Thanks. I have a question though, as one of the users pointed out in one of the answers above, will there be any memory issue if I use this technique in a series of fragments? – Tony Mathew Sep 27 '16 at 10:41
  • It will create issue when you add fragment ,in your case you are replacing fragment so it wont be getting any issue – Burhanuddin Rashid Sep 27 '16 at 10:57
0

When you have a series of fragments if you add them to the backstack it would be memory wastage. For this kind of action android provides something called ViewPager, which will always store 2 fragments at a time and create the rest of fragments dynamically when needed.

So if you still decided you don't want to use ViewPager, you can store the Fragment's data in activity's bundle and you can retrieve data when creating the fragment again

Mohanakrrishna
  • 148
  • 3
  • 13