1

ユーザーが終了したときにアクティビティの状態を保存し、次回同じ画面で再起動したい。

いくつかのチュートリアルを分類して配置し、難易度ボタンを押すと、より多くのボタンとチュートリアルの名前が表示された画面に移動します。いずれかを押すと、スクロールビューにテキストが表示されたアクティビティが開きます。

ユーザーが外出したときにどこにいたかを保存するにはどうすればよいですか?

ユーザーが短い説明を読んでいるとします。その後、そのユーザーは立ち去る必要があり、押し返し、最終的には終了します...しかし、ユーザーは短い説明の途中でした。中断したところから続行できるようにしたいのですが... どうすればいいですか?

https://groups.google.com/forum/#!forum/android-developersからここに来ました

ここで初歩的な質問をしてもいいとのことですが、人々は貴重な時間を意地悪に費やしているようです。それは本当に価値がありますか?知っていれば、助けてください。(コード例は素晴らしいでしょうが、助けていただければ幸いです)

4

1 に答える 1

3

Activity からonSaveInstanceStateonRestoreInstanceStateを使用できます。

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 を呼び出し、現在のチュートリアル ページと、ユーザーが完了していないことを示すブール値を保存します。それ。

ユーザーが後でアプリを再起動すると、最初に onRestoreInstanceState がチェックされ、フィールド「savedInstanceState」が存在するかどうかがチェックされます。そうでない場合は、 onCreate() メソッドに進みます。その後、アプリは init() メソッドで開始されました。

「YourActivity」は、プロジェクト/アプリの名前に置き換えることができます。

追加情報: 渡されたデータを自分で処理する必要があります。チュートリアルを処理する別のクラス (fe ページのゲッター/セッター) に渡します。

于 2013-07-19T11:59:02.703 に答える