Tony Mathew
Tony Mathew

Reputation: 191

Save and restore fragment state

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;
}
}

Upvotes: 2

Views: 2908

Answers (2)

Mohanakrrishna
Mohanakrrishna

Reputation: 168

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

Upvotes: 0

Burhanuddin Rashid
Burhanuddin Rashid

Reputation: 5370

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();

Upvotes: 3

Related Questions