ActivityのonSaveInstanceState呼び出しタイミングについての説明

5275 ワード

Activity           onSaveInstanceState   ,                  Activity     ,                       ,           ,  
    :android-sdk-windows-1.5_r3/docs/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)

protected void onSaveInstanceState (Bundle outState)
       Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both). This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle).
 
各インスタンスを保存するステータスは、activityが殺される前に呼び出され、onCreate(Bundle)またはonRestoreInstanceState(Bundle)(入力されたBundleパラメータはonSaveInstanceStateによってカプセル化された)でリカバリできることを保証します.この方法はactivityが殺される前に呼び出され、activityが将来のある時点で戻ってきたときに以前の状態を回復することができる.たとえば、activity Bが有効になってactivity Aの先頭にある場合、ある時点でactivity Aがシステム回収リソースの問題で殺されると、AはonSaveInstanceStateを介してそのユーザインタフェースの状態を保存する機会があり、将来のユーザがactivity Aに戻ったときにonCreate(Bundle)またはonRestoreInstanceState(Bundle)を介してインタフェースの状態を回復することができる.
 
    Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact. この方法をonPause()やonStop()のようなactivityライフサイクルコールバックと混同しないでください.onPause()はactivtiyが背景に置かれたり、自分で破棄されたりしたときに呼び出され、onStop()はactivityが破棄されたときに呼び出されます.onPause()とonStop()が呼び出されますが、onSaveInstanceStateがトリガーされない例は、ユーザーがactivity Bからactivity Aに戻ると、BのonSaveInstanceState(Bundle)を呼び出す必要がなく、このときのBインスタンスが回復しないため、システムは呼び出さないようにします.onPause()を呼び出すがonSaveInstanceStateを呼び出さない例は、activity Bが起動してactivity Aのフロントエンドにある場合です.Bのライフサイクル全体でAのユーザーインタフェース状態が破壊されなければ、activity AのonSaveInstanceState(Bundle)は呼び出されません.      The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself. If called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().
 
デフォルトのインプリメンテーションは、UIレイヤ上のidを持つviewのonSaveInstanceState()を呼び出し、現在フォーカスされているviewのid(保存されているすべてのステータス情報がデフォルトのonRestoreInstanceState(Bundle)インプリメンテーションで復元される)を保存するUIインスタンスステータスの大部分を担当します.この方法を上書きして、各viewに保存されていない追加の情報を保存する場合は、デフォルトの実装中に各ビューのすべてのステータスを呼び出したり、自分で保存したりしたい場合があります.呼び出されると、このメソッドはonStop()の前にトリガーされますが、onPause()の前または後にトリガーされるかどうかは保証されません.
Activityクラスに含まれるonSaveInstanceStateとonRestoreInstanceStateが何の役に立つのか分からないことが多いので、まず、この2つのメソッドを使用する場合は、Activityのライフサイクルに注意して理解する必要があります.そうしないと、onSaveInstanceStateとonRestoreInstanceStateはトリガーされない場合があります.Activityの書き換え方法ですが.(文/Android開発網)彼らがよく使うのは、Sensor、Land、Portレイアウトの自動切り替えです.過去にAndroid開発網は、横画面と縦画面の切り替えによるデータが空になったり、onCreateが繰り返し呼び出されたりする問題を解決したと言っていましたが、Androidが提供するonSaveInstancesStateメソッドでは、現在のウィンドウステータスがレイアウト切り替え前または現在のActivityが履歴スタックにプッシュされていることを保存できます.実際にはレイアウト切り替えもonPauseを呼び出したことがあるのでActivityのhistory stackにプッシュされます.Activityがバックグラウンドで実行メモリが詰まっていない場合は、スイッチバックするとonRestoreInstanceStateメソッドがトリガーされます.この2つの方法のパラメータはBundleであり、SharedPreferencesのようなデータを格納することができるので、現在のウィンドウの状態として保存するのに適しています.実際の使用コード
  • @Override
  •   protected void onSaveInstanceState(Bundle outState){
  •             outState.putString("lastPath", "/sdcard/android123/cwj/test");
  •   }
  • @Override
  • public void onRestoreInstanceState(Bundle savedInstanceState) {
  • super.onRestoreInstanceState(savedInstanceState);
  • String cwjString = savedInstanceState.getString("lastPath");
  • }