0

I have FragmentX in a ViewPager. FragmentX has EditText's in it and a Button. The user presses the Button and FragmentY replaces FragmentX. The user then presses back and FragmentX has lost all of its input from the user.

How do you either:

a) Save the data in FragmentX before FragmentY appears then FragmentY is replaced by FragmentX retrieve the data and fill in the EditText's in FragmentX

(please don't reply with OnSaveInstanceState, as this does not work unless the Fragment is destroyed, which in this case it is not)

b) somehow keep the data in FragmentX so it is there when we go back to FragmentX from FragmentY..

Any suggestions?

Nickmccomb
  • 1,467
  • 3
  • 19
  • 34

3 Answers3

0

I suggest following guidelines that Google provides and implement an interface declared in your Fragment and save the Bundle or whatever object you want in the activity. Then, in your newInstace() static factory method pass that Bundle and recreate data as usual. Since you are using a ViewPager and it will always render the second fragment before the button is pushed (I assume your second fragment is in another tab) you still need to manage it via an interface. When the back button is pressed, the data will still be there, unless it is destroyed, and you still need to implement onSaveInstaceState() for that matter. You can also use setRetainInstance(boolean retain). See here for more details

Chisko
  • 3,092
  • 6
  • 27
  • 45
0

Using addToBackStack() might help in your case. If you return to a fragment from the back stack it does not re-create the fragment but re-uses the same instance and starts with onCreateView() in the fragment lifecycle, see Fragment lifecycle.

So if you want to store state you should use instance variables and not rely on onSaveInstanceState().

Check this out: Maintaining fragment states

Community
  • 1
  • 1
Praveen Kishore
  • 436
  • 1
  • 3
  • 14
  • i'm using addToBackStack(), the problem is that adding to the backstack doesn't save anything that the user has input into the Fragment, so when the Fragment is called from the backstack and shown, the data is gone. – Nickmccomb Jun 28 '16 at 04:31
  • `getSupportFragmentManager().beginTransaction().addToBackStack("fragment1").replace(R.id.mainFragmentHolder, new YourFragment()).commit();` Should work. Can you pls include the code that calls the fragment. The data should remain if you are _not overriding the default behavior_. – Praveen Kishore Jun 28 '16 at 04:45
  • please double check. That answer is outdated. There is no way to do that, because of the way ViewPager works, please see my updated answer – Chisko Jun 28 '16 at 06:48
0

I am now saving the data in FragmentX to SharedPreferences and overriding onBackPressed in the Activity, and have created a function in the Activity called popFromBackstack where the popBackStack() occurs.

In the functions in my Activity where i replace my Fragments i am now saving the data in FragmentX into SharedPrefs before the replace. I am also checking after the replace if the new Fragment is FragmentX and, if it is, i am filling the data into FragmentX from SharedPrefs.

I am also saving the data from FragmentX in onBackPressed in my Activity (if the current fragment is FragmentX), incase the user presses the back button.

I have also created a public static activity called popBackStack() in my Activity which i call from Fragments to pop the backstack. I am also saving the data from FragmentX here (if the current fragment being popped is FragmentX). Once the Fragment being popped is popped i am checking if the new Fragment is FragmentX, and filling in the data if it is...

Long winded approach but i couldn't figure out any other reliable way. This is working perfectly.

Nickmccomb
  • 1,467
  • 3
  • 19
  • 34