Activity から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 を呼び出し、現在のチュートリアル ページと、ユーザーが完了していないことを示すブール値を保存します。それ。
ユーザーが後でアプリを再起動すると、最初に onRestoreInstanceState がチェックされ、フィールド「savedInstanceState」が存在するかどうかがチェックされます。そうでない場合は、 onCreate() メソッドに進みます。その後、アプリは init() メソッドで開始されました。
「YourActivity」は、プロジェクト/アプリの名前に置き換えることができます。
追加情報: 渡されたデータを自分で処理する必要があります。チュートリアルを処理する別のクラス (fe ページのゲッター/セッター) に渡します。