Saving intent data in android

I am creating a simple poll app in android studio. I have multiple intentions and I run each one when the Next buttons are clicked. I also have a back button that takes the user to the previous intent if they want to change the answer to a question. When I go ahead again, none of the answers are saved. How can I get answers to all user responses?

+3


source to share


4 answers


You can override two methods in your actions:

public void onSaveInstanceState(Bundle savedInstanceState);
public void onRestoreInstanceState(Bundle savedInstanceState);

      

As you can see, you have access to the "savedInstanceState" Bundle. If the user terminates the onSaveInstanceState () action is called and you have the option to save the data you want to restore later.

The onRestoreInstanceState () method is only called if you have saved information in onSaveInstanceState () and offers you the option to restore this data. It provides a collection that contains the same data that you saved in the data during onSaveInstanceState ().

The package is basically a list of key values. To store a string, for example, you simply call the putString () method - the package method:



savedInstanceState.putString("myKey", "Hello World");

      

There are many types of data that can be stored in a bundle (in general, every class that implements the Parcelable interface can be stored in a bundle).

Additional Information:

http://developer.android.com/training/basics/activity-lifecycle/recreating.html http://developer.android.com/reference/android/os/Bundle.html

+3


source


How about using a singleton class for your data and storing it there in a list or array. You can extend the Application class to do this, or just create your own singleton class.



0


source


When your actions are recreated, they receive an onCreate (Bundle) callback . Whatever you save in onSaveInstanceState (Bundle) will appear in the Bundle.

0


source


You can implement a singleton class and use it in your application that extends Application . Store your data in singleton mode across the map and provide getter and setter methods.

-1


source







All Articles