简体   繁体   中英

Android how to save all views in Fragment when user click on next and prev button

i use fragment for my application,they are : Fragment A, Fragment B and Fragment C.

to move from one fragment to another fragment, I use two buttons, the first button is used to move to the next fragment, and the second button is used to return to the previous fragment.

the problems when I move from one fragment to another fragment (such as from A to B and back to A and to B again and to C). all entries in the EditText that is in a fragment is lost and look back empty,it when I go back to the previous fragment, or when I want to go back to the next fragment. I've been using code addToBackStack () when switching to another fragment, indeed EditText that I made not lost when I press the back button on my phone, but it does not affect the buttons (Next And Prev) that I've created. this is Fragment A:

public class AFragment extends Fragment {
EditText text,text2;
public AFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_a, container, false);
    ImageView next=(ImageView)rootView.findViewById(R.id.next);
    text=(EditText)rootView.findViewById(R.id.text);
    text2=(EditText)rootView.findViewById(R.id.text1);
    next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

              getFragmentManager().beginTransaction().replace(R.id.frame_container, new BFragment()).addToBackStack(null).commit();


        }
    });
    return rootView;
}

this is Fragment B:

public class BFragment extends Fragment {
EditText cut,cut2;
public BFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View Fragment B = inflater.inflate(R.layout.fragment_c, container, false);
    ImageView next=(ImageView)rootView.findViewById(R.id.next);
    ImageView prev=(ImageView)rootView.findViewById(R.id.prev);
    cut=(EditText)rootView.findViewById(R.id.cut);
    cut2=(EditText)rootView.findViewById(R.id.cut2);
    next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

              getFragmentManager().beginTransaction().replace(R.id.frame_container, new CFragment()).addToBackStack(null).commit();


        }
    });
    prev.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

              getFragmentManager().beginTransaction().replace(R.id.frame_container, new AFragment()).addToBackStack(null).commit();


        }
    });
    return Fragment B;
}

my question is how can i save view in Fragment when user click on next and prev button in my application? is there something missing in my code? i hope someone can help me to solve my problem. thank you very much.

Each fragments have various life cycle methods, you can use onDestroy method and save the required edit text view value calling Activity. For this expose some methods in the calling activity which can be access via fragment. Also else create a singleton class or use Shared Prefences.

I hope this helps.

try to change:

getFragmentManager().beginTransaction().replace(R.id.frame_container, new BFragment()).addToBackStack(null).commit();

to:

getFragmentManager().beginTransaction().add(R.id.frame_container, new BFragment()).addToBackStack(null).commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM