如何继续当用户离开?继续、用户

2023-09-04 03:46:15 作者:叼着奶嘴上青楼

我要救的活动状态,当用户退出,然后再次在同一屏幕下一次启动它。

I want to save the activity state when the user quits,then start it up again at the same screen next time.

我只是有一些教程斩钉截铁地放置,preSS困难按钮,你会被带到一个屏幕,更多的按钮和教程名称。 preSS的任何一个,并与在滚动视图只是一些文本活动将打开。

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 ?

假设一个用户正在阅读一个啧啧,那么他/她需要离开,他presses回来,并最终退出......但他们在啧啧的中间。我希望他们能够继续他们离开的地方......这怎么办?

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?

我来到这里,从 https://groups.google.com/forum/ #!论坛/ Android的开发者

它说,我们可以让初学者问题在这里,但人们似乎宁愿花自己的precious暂时的意思。是不是真的值得吗?如果你知道请帮我出。 (A code的例子将是巨大的,但任何帮助将是AP preciated)

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)

推荐答案

您可以使用的onSaveInstanceState 和 onRestoreInstanceState 的活动。

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);
        }
}

如果用户启动了第一时间的应用程序,所以,它会调用的onCreate()方法。在那里,你已经初始化教程页面。用户经过了10页,但他停止并在离开第6页应用好了,那一刻,应用程序将调用的onSaveInstanceState将保存当前教程页面和一个布尔值,指出用户没有完成它。

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.

当用户启动应用程序再次后,它会检查onRestoreInstanceState首先,如果场savedInstanceState存在检查。如果不是,则继续的onCreate()方法。 然后应用程序开始了在init()方法。

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可以更换,以你的项目/应用程序的名称。

"YourActivity" can be replaced to your project/app's name.

另外的信息:你必须做一些与传递的数据自己。它传递给它处理教程(getter / setter方法​​对Fe页)一个独立的类。

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