How to continue where the user left off?

前端 未结 1 1771
悲&欢浪女
悲&欢浪女 2021-01-25 01:44

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 diff

相关标签:
1条回答
  • 2021-01-25 02:21

    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).

    0 讨论(0)
提交回复
热议问题