问题
I want to save the activity state when the user quits,then start it up again at the same screen next time.
I just have a few tutorials categorically placed, press a difficulty button , you'll be taken to a screen with more buttons and names of tutorials. Press any one and an activity with just some text in a scrollview will open up .
How do I save where the user was when they went off ?
Say a user is reading a tut, then he/she needs to leave, he presses back and eventually exits...but they were in the middle of the tut. I want them to be able to continue where they left off... How to do this?
I came here from https://groups.google.com/forum/#!forum/android-developers
It said we can ask beginner questions here, but people seem to rather spend their precious time being mean. Is it really worth it ? If you know pls help me out. (A code example would be great, but any help would be appreciated)
回答1:
you can use the onSaveInstanceState and onRestoreInstanceState from Activity.
public class YourActivity extends Activity {
private boolean tutorialUsed;
private int tutorialPage;
/** this will be called when you launch the app */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// place default field values here below if you have private variables - always at first
tutorialUsed = false;
tutorialPage = 1;
// your onCreate method/tasks (when you start this application)
init(); // see below *%* for details
}
/** this will be called when the app closes */
@Override
protected void onDestroy() {
super.onDestroy();
}
/** this will be called when you switch to other app (or leaves it without closing) */
@Override
protected void onPause() {
super.onPause();
// pauze tasks
}
/** this will be called when you returns back to this app after going into pause state */
@Override
protected void onResume() {
super.onResume();
// resume tasks
}
/** this starts when app closes, but BEFORE onDestroy() */
// please remember field "savedInstanceState", which will be stored in the memory after this method
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// save the tutorial page (or something else)
savedInstanceState.putInt("TutPage", tutorialPage);
savedInstanceState.putBoolean("tutUsed", tutorialUsed);
// more additions possible
}
/** this starts after onStart(). After this method, onCreate(Bundle b) gets invoked, followed by onPostCreate(Bundle b) method
* When this method has ended, the app starts skipping the onCreate and onPostCreate method and initiates then.
* -- *%* A best practice is to add an init() method which have all startup functions.
*/
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// restore state
tutorialPage = savedInstanceState.getInt("TutPage");
tutorialUsed = savedInstanceState.getBoolean("tutUsed");
init();
}
/** do your startup tasks here */
public void init() {
if(!tutorialUsed) {
showTutorial(tutorialPage);
}
}
So if the user starts the app for the 1st time, it will invoke onCreate() method. There, you have initialized the tutorial page. the user goes through the 10 pages, but he stops and leaves the application at page 6. Well, on that moment, the app will invoke onSaveInstanceState which will save the current tutorial page and a boolean which says that the user didn't have completed it.
When the user starts the app again later, it checks onRestoreInstanceState firstly and checks if the field "savedInstanceState" exists. If not, it continues to onCreate() method. Then the app got started at init() method.
"YourActivity" can be replaced to your project/app's name.
additional info : you have to do something with the passed data yourself. Pass it to a separate class which handles the tutorial (getters/setters on fe page).
来源:https://stackoverflow.com/questions/17744765/how-to-continue-where-the-user-left-off