Reputation: 31
I am creating a simple survey app in android studio. I have multiple intents, and I start each one when the "next" buttons are pressed. I also have a back button that brings the user to the previous intent if they want to change an answer to a question. When I go forward again, none of the answers are saved. How can I get all the users answers to be maintained?
Upvotes: 3
Views: 1890
Reputation: 2124
There are two methods you can override in your activities:
public void onSaveInstanceState(Bundle savedInstanceState);
public void onRestoreInstanceState(Bundle savedInstanceState);
As you see you have access to the Bundle "savedInstanceState". If a user terminates an activity onSaveInstanceState() gets called and you have the possibility to save data which you want to restore later.
The method onRestoreInstanceState() only gets called if you have saved information in onSaveInstanceState() and offers you the chance to restore that data. It provides a bundle which contains the same data that you saved into the data during onSaveInstanceState().
A bundle is basically a key-value list. To store a string for example you simply would call the putString()-Method of the bundle:
savedInstanceState.putString("myKey", "Hello World");
There are many data-types that you can store in a bundle (in general, every class that implements the interface Parcelable can be stored in a bundle).
More information:
http://developer.android.com/training/basics/activity-lifecycle/recreating.html http://developer.android.com/reference/android/os/Bundle.html
Upvotes: 3
Reputation: 1
You can implement a singleton class ,and use it in your application which extends Application. Store your data to the singleton by map and provide getter and setter methods
Upvotes: -1
Reputation: 69198
When your Activities are recreated they receive the callback onCreate(Bundle). Whatever you have saved in onSaveInstanceState(Bundle) will appear in Bundle.
Upvotes: 0
Reputation: 350
How about using a singleton class for your data and store the intends there in a List or Array. You can extend the Application class for this or just make your own singleton class.
Upvotes: 0