Locked History Actions

Diff for "Android/Activity"

Differences between revisions 8 and 9
Deletions are marked like this. Additions are marked like this.
Line 16: Line 16:
実際にデバッガ使ってみると、説明の通り、BのonCreateが呼び出されるのは、AのonPauseの後である。つまり、以下の順序である。 実際にトレースおこなってみると、説明の通り、BのonCreateが呼び出されるのは、AのonPauseの後である。つまり、以下の順序である。

Activty

Activityのライフサイクルに関する間違い

http://developer.android.com/reference/android/app/Activity.html

に以下のような図があるが、

http://developer.android.com/images/activity_lifecycle.png

onPauseの前の「Another Activity comes in front of the activity」という部分は間違い、あるいは間違いで無いとしても非常に紛らわしい。 onPauseが呼び出される以前には、Another Activityオブジェクトは生成されてもいないのである。これは、後述のonPauseメソッドの説明からも明らかである。 翻訳:「AのonPauseから返ってこない限りBはcreateされないため、ここで長い処理は行ってはならない」。

実際にトレースをおこなってみると、説明の通り、BのonCreateが呼び出されるのは、AのonPauseの後である。つまり、以下の順序である。

  • A.onPause
  • B.onCreate
  • B.onStart
  • B.onResume
  • A.onStop

したがって、A.onPause以前にBオブジェクト自体は作成されて可能性はあるかもしれないが、B.onCreateが呼び出されるのはA.onPauseの後であるため、 A.onPause以前にBが実行可能状態になることは絶対に無い。

考えてみればこれは当たり前のとで、A.onPause以前にBが「実行」されてしまうと、同時に二つのActivityが動作することになってしまう。 これではシステム的にアプリケーションプログラミング的にも複雑になってしまう。

これらをきちんと確認せずに「Activityのライフサイクル」を上の間違った図を元にして説明しているサイトが異常に多い(というよりほとんど)ので注意が必要である。

Activityのライフサイクル

※以下はhttp://developer.android.com/reference/android/app/Activity.htmlの一部の翻訳

onCreate

protected void onCreate (Bundle savedInstanceState) Since: API Level 1

Called when the activity is starting. This is where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById(int) to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri, String[], String, String[], String) to retrieve cursors for data being displayed, etc.

You can call finish() from within this function, in which case onDestroy() will be immediately called without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown. Parameters savedInstanceState If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null. See Also

  • onStart()
  • onSaveInstanceState(Bundle)
  • onRestoreInstanceState(Bundle)
  • onPostCreate(Bundle)

onRestart

onStop()の呼び出し後、現在のアクティビティが再表示されたとき(ユーザがそこにバックしたとき)に呼び出される。 引き続き、onStart(), onResume()が呼び出される。

もしアクティビティがRaw Cursorオブジェクト(managedQuery(android.net.Uri, String[], String, String[], Stringの代わりに)を使用しているのであれば、 この場所が通常は再クエリを行うのにふさわしい場所である(なぜなら、onStop()にて非アクティブにしているであろうから)。

派生クラスはスーパークラスの同メソッドを呼び出さなければならない、さもないと例外が発生する。

onStart

protected void onStart () Since: API Level 1

Called after onCreate(Bundle) — or after onRestart() when the activity had been stopped, but is now again being displayed to the user. It will be followed by onResume().

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown. See Also

  • onCreate(Bundle)
  • onStop()
  • onResume()

onResume

protected void onResume () Since: API Level 1

Called after onRestoreInstanceState(Bundle), onRestart(), or onPause(), for your activity to start interacting with the user. This is a good place to begin animations, open exclusive-access devices (such as the camera), etc.

Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user (for example, to resume a game).

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown. See Also

  • onRestoreInstanceState(Bundle)
  • onRestart()
  • onPostResume()
  • onPause()

onPause

アクティビティがバックグラウンドに移行しようとするとき(ただし、まだkillされていない)、アクティビティのライフサイクルの一部として呼び出される。 onResume()の逆である。

アクティビティAの上にアクティビティBが起動されたとき、Aにおいてこのコールバックが起動される。 AのonPauseから返ってこない限りBはcreateされないため、ここで長い処理は行ってはならない。

This callback is mostly used for saving any persistent state the activity is editing, to present a "edit in place" model to the user and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one.

アニメーションやその他のCPUを消費しがちな処理を停止するためのタイミングとしても良い場所である。 なぜなら、次のアクティビティにできる限り早く移行するため、あるいはカメラのような排他的リソースに対するアクセスをクローズする必要があるからである。

システムがメモリを必要とする場合には、pauseされたプロセスをkillする場合がある。 このため、このメソッドから戻る時には、必要なすべての状態がセーブされていなければならない。 一般的には、アクティビティ中の永続的状態をセーブするにはonSaveInstanceState(Bundle)が使用され、 このメソッドはグローバルな永続的データをContent Providerやファイル等保存するのに使用される。

この呼び出しを受けた後、通常はそれにひき続いてonStopの呼び出しを受けることになる (そのまえに次のアクティビティがresumeされて表示されるが)。 しかしながら、stop状態にならずにそのままonResumeが呼び出されることもある。

※訳注:どのような場合にこれが発生するのか何も説明が無い。

派生クラスはスーパークラスの同メソッドを呼び出す必要がある。そうしなければ例外が発生する。

onStop

非表示になったときに呼び出される。このメソッドの次はonRestart()あるいはonDestroy()が呼び出されるか、あるいは何も呼び出されない。 これは後のユーザの操作による。

このメソッドが全く呼び出されない場合もありうる。システムのメモリ不足状態でonPauseの呼び出し後にもはやアクティビティのプロセス状態を保持できなくなった場合である。

派生クラスはスーパークラスの同メソッドを呼び出さなければならない。そうしなければ例外が発生する。

onDestyroy

protected void onDestroy () Since: API Level 1

Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown. See Also

  • onPause()
  • onStop()
  • finish()
  • isFinishing()