android - How to continue where the user left off? -


i want save activity state when user quits,then start again @ same screen next time.

i have few tutorials categorically placed, press difficulty button , you'll taken screen more buttons , names of tutorials. press 1 , activity text in scrollview open .

how save user when went off ?

say user reading tut, he/she needs leave, presses , exits...but in middle of tut. want them able continue left off... how this?

i came here https://groups.google.com/forum/#!forum/android-developers

it said can ask beginner questions here, people seem rather spend precious time being mean. worth ? if know pls me out. (a code example great, appreciated)

you can use onsaveinstancestate , onrestoreinstancestate activity.

public class youractivity extends activity {      private boolean tutorialused;     private int tutorialpage;      /** called when launch app */     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          // place default field values here below if have private variables - @ first         tutorialused = false;         tutorialpage = 1;          // oncreate method/tasks (when start application)         init(); // see below *%* details     }      /** called when app closes */     @override     protected void ondestroy() {         super.ondestroy();     }      /** called when switch other app (or leaves without closing) */     @override     protected void onpause() {         super.onpause();         // pauze tasks     }      /** called when returns app after going pause state */     @override     protected void onresume() {         super.onresume();         // resume tasks     }      /** starts when app closes, before ondestroy() */     // please remember field "savedinstancestate", stored in memory after method     @override     public void onsaveinstancestate(bundle savedinstancestate) {         super.onsaveinstancestate(savedinstancestate);         // save tutorial page (or else)         savedinstancestate.putint("tutpage", tutorialpage);         savedinstancestate.putboolean("tutused", tutorialused);         // more additions possible     }      /** starts after onstart(). after method, oncreate(bundle b) gets invoked, followed onpostcreate(bundle b) method     * when method has ended, app starts skipping oncreate , onpostcreate method , initiates then.     * -- *%* best practice add init() method have startup functions.     */     @override     public void onrestoreinstancestate(bundle savedinstancestate) {         super.onrestoreinstancestate(savedinstancestate);         // restore state         tutorialpage = savedinstancestate.getint("tutpage");         tutorialused = savedinstancestate.getboolean("tutused");         init();     }      /** startup tasks here */     public void init() {         if(!tutorialused) {             showtutorial(tutorialpage);         } } 

so if user starts app 1st time, invoke oncreate() method. there, have initialized tutorial page. user goes through 10 pages, stops , leaves application @ page 6. well, on moment, app invoke onsaveinstancestate save current tutorial page , boolean says user didn't have completed it.

when user starts app again later, checks onrestoreinstancestate firstly , checks if field "savedinstancestate" exists. if not, continues oncreate() method. app got started @ init() method.

"youractivity" can replaced project/app's name.

additional info : have passed data yourself. pass separate class handles tutorial (getters/setters on fe page).


Comments