-1

I'm using a ViewPager and a FragmentManager to fill some EditText and click on next button to go ahead. But when I click on the previous button to go back to the last fragment, the information inside the form are lost, because I always re-instantiate each fragment like this:

 void previousFragment(){

    Fragment newFragment = new RegisterLink_();
    final FragmentManager fm = getActivity().getSupportFragmentManager();
    final FragmentTransaction ft = fm.beginTransaction();

    ft.setCustomAnimations(R.anim.in, R.anim.out);
    ft.replace(R.id.register_upload, newFragment);
    ft.addToBackStack(null);
    ft.commit();
   }
}

Can someone please tell me how to swipe from a fragment to another one without losing the information inserted by the user. Thanks for your help and sorry for my weak english.

Nizar B.
  • 3,098
  • 9
  • 38
  • 56

3 Answers3

2

when you navigate between fragment, to preserve data in this cases you can do the following

  1. Have a global variable to hold data, so when you navigate you still have data entered by user
  2. Or before switching fragment, wrap the data into bundle and pass to next fragment using setArgument method same you can retrieve by getArgument in next fragment.

So by any of above when you have preserved your data, you can always initialize all the form fields with proper values entered by user.

Techfist
  • 4,314
  • 6
  • 22
  • 32
0

before calling the previous fragment you have to extract the data from the current fragment. Create a method in the fragment that gives you that value.

FragmentManager fragmentManager = getFragmentManager();
YOUR_FRAGMENT f = (YOUR_FRAGMENT)fragmentManager.findFragmentByTag(THE_TAG);
f.theNewMethod(); // store this!
Pedro Bernardo
  • 973
  • 2
  • 8
  • 17
0

if you want to back to previous fragment using a button, just add this code on your button :

void previousFragment(){
getFragmentManager().popBackStack();
            getFragmentManager().executePendingTransactions();
}

and then you'll not lose your last edit on your previous fragment

Aoyama Nanami
  • 2,532
  • 9
  • 32
  • 50