0

I'm newbie in android and I'm working with fragments.

My problem:

I have two fragments, "A" and "B", my fragment "A" is a complex form, the user can add and remove products, set customer information, payment method and more. My fragment "B" just show the information that user set in the previous fragment.

But if user press back button, I'm losing all information that already set in the previous fragment.

How can i maintain this informations on fragment A?

Thanks.

madhan kumar
  • 1,560
  • 2
  • 26
  • 36

2 Answers2

1

you can save your data by using sharedpreferences

so that once you get back to the fragment you load your data from sharedpreferences

has19
  • 1,307
  • 16
  • 31
1

You should add android:id attribute to each View element of which state you want to save. this explains in android documentation:

By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText object). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you. However, your activity might have more state information that you'd like to restore, such as member variables that track the user's progress in the activity.

Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.

To save additional data about the activity state, you must override the onSaveInstanceState() callback method. The system calls this method when the user is leaving your activity and passes it the Bundle object that will be saved in the event that your activity is destroyed unexpectedly. If the system must recreate the activity instance later, it passes the same Bundle object to both the onRestoreInstanceState() and onCreate() methods.

konstantin
  • 19
  • 1