[Andriod公式トレーニングチュートリアル]Activityのライフ活動を管理してActivityを再作成

9900 ワード

原文住所:https://developer.android.com/training/basics/activity-lifecycle/recreating.html
-----------------------------------------------------------------------------------------------------------------------
There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses theBack button or your activity signals its own destruction by calling finish() . The system may also destroy your activity if it's currently stopped and hasn't been used in a long time or the foreground activity requires more resources so the system must shut down background processes to recover memory.
通常のapp動作のためにactivityが破棄される場合があります.例えば、ユーザーが戻るボタンを押すと、またはactivityがfinish() 。 activity , activity , activity。を呼び出した場合があります.
When your activity is destroyed because the user presses Back or the activity finishes itself, the system's concept of that Activity instance is gone forever because the behavior indicates the activity is no longer needed. However, if the system destroys the activity due to system constraints (rather than normal app behavior), then although the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed. The saved data that the system uses to restore the previous state is called the "instance state"and is a collection of key-value pairs stored in a Bundle object.
ユーザーが戻るボタンを押したためにactivityが自分で破棄されると、システム内のActivityインスタンスは永久に消えます.このような動作はactivityが不要になったことを示しています.ただし、システムがactivityを破棄するのは、通常のapp動作ではなくシステム制約のためである場合、実際のActivityインスタンスは消えているにもかかわらず、システムはその存在を覚えています.これにより、ユーザーがナビゲートして戻ると、保存されたデータ(activityが破棄された状態を示す)を使用して新しいactivityインスタンスが作成されます.システムが以前の状態を再読み込みするために保存したデータは、Bundleオブジェクトに格納されたキー値ペアのセットである「インスタンス状態」と呼ばれます.
Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).
警告:ユーザーが画面を回転するたびにactivityが破棄され、再構築されます.画面が方向を変更すると、フロントのactivityが破棄され、再構築されます.画面の設定が変更されたため、activityはレイアウトなどの他の資料をロードする必要がある場合があります.
By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText object). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you. However, your activity might have more state information that you'd like to restore, such as member variables that track the user's progress in the activity.
デフォルトでは、Bundleインスタンスステータスを使用して、EditTextオブジェクトに入力されたテキスト値などのactivityレイアウトの各Viewに関する情報を保存します.したがって、activityインスタンスが破棄されて再構築されると、コードを書く必要がなく、レイアウトのステータスが以前のステータスとして再ロードされます.しかし、activityでは、activityでユーザープロセスを追跡するメンバー変数など、より多くの情報をロードする必要がある場合があります.
Note: In order for the Android system to restore the state of the views in your activity,each view must have a unique ID, supplied by the android:id attribute.
注意:Androidシステムがactivityのviewsの状態を再ロードするには、android:idのプロパティで提供される一意のIDが必要です.
To save additional data about the activity state, you must override the onSaveInstanceState() callback method. The system calls this method when the user is leaving your activity and passes it the Bundle object that will be saved in the event that your activity is destroyed unexpectedly. If the system must recreate the activity instance later, it passes the same Bundle object to both the onRestoreInstanceState() and onCreate() methods.
activityステータスに関する追加のデータを保存するには、onSaveInstancesState()コールバックメソッドを再ロードする必要があります.ユーザーがactivityを離れる準備をしていると、このメソッドが呼び出され、Bundleオブジェクトに渡され、activityが意外に破棄されたときに保存されます.
Figure 2. As the system begins to stop your activity, it calls onSaveInstanceState() (1) so you can specify additional state data you'd like to save in case the Activity instance must be recreated. If the activity is destroyed and the same instance must be recreated, the system passes the state data defined at (1) to both the onCreate() method (2) and the onRestoreInstanceState() method (3).
図2.システムがactivityを停止し始めると、onSaveInstanceState()(1)が呼び出されます.したがって、Activityインスタンスが再構築されなければならない場合は、保存したい追加のステータスを示すことができます.activityが破棄され、同じインスタンスが再構築される必要がある場合、システムは(1)で定義された状態をonCreate()メソッド(2)およびonRestoreInstanceState()メソッド(3)に渡す.

Save Your Activity State-アクティブな状態を保存


As your activity begins to stop, the system calls onSaveInstanceState() so your activity can save state information with a collection of key-value pairs. The default implementation of this method saves information about the state of the activity's view hierarchy, such as the text in an EditText widget or the scroll position of a ListView .
activityが停止すると、onSaveInstanceState()が呼び出されるので、activityはキー値ペアでステータス情報を保存できます.この方法のデフォルトインプリメンテーションは、EditTextのテキストやListViewのスクロール位置などのactivityのviewの階層情報を保存する.
To save additional state information for your activity, you must implement onSaveInstanceState() and add key-value pairs to the Bundle object. For example:
activityの追加情報を保存するには、onSaveInstanceState()を実装し、Bundleオブジェクトにキー値ペアを追加する必要があります.例:
static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
    
    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

Caution: Always call the superclass implementation of onSaveInstanceState() so the default implementation can save the state of the view hierarchy.
警告:view階層のステータスを保存できるため、onSaveInstancesState()の親インプリメンテーションは常に呼び出されます.

Restore Your Activity State-アクティブな状態を再ロード


When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system passes your activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.
あなたのactivityが破棄されて再構築されると、Bundleから保存された状態を回復することができます.onCreate()とonRestoreInstanceState() Bundle 。Because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed.
onCreate()メソッドは、新しいインスタンスを作成し、以前のインスタンスを再構築するために呼び出されるため、Bundleを読もうとする前にnullの状態かどうかを確認する必要があります.nullの場合、再ロード前に破棄されたインスタンスではなく、新しいactivityインスタンスが作成されます.
For example, here's how you can restore some state data in onCreate() :
たとえば、onCreate()で同じステータスデータをどのように再ロードできるかを示します.
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first
   
    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

Instead of restoring the state during onCreate() you may choose to implement onRestoreInstanceState() , which the system calls after the onStart() method. The system calls onRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether the Bundle is null:
onCreate()のステータスを再ロードするのではなく、onRestoreInstanceState()(システムがonStart()メソッドの後に呼び出される)を実装することを選択することができます.システムは、onRestoreInstanceState()を呼び出すのは、保存されたステータスがリロードされる必要がある場合のみです.
public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);
   
    // Restore state members from saved instance
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}

Caution: Always call the superclass implementation of onRestoreInstanceState() so the default implementation can restore the state of the view hierarchy.
警告:デフォルトのインプリメンテーションはview階層のステータスを再ロードできるため、onRestoreInstanceState()の親インプリメンテーションは常に呼び出されます.
To learn more about recreating your activity due to a restart event at runtime (such as when the screen rotates), readHandling Runtime Changes.
実行時に再起動イベントが発生したときにactivityを再構築する方法については、Handling Runtime Changesを参照してください.