Revision 2 as of 2010-10-25 07:40:24

Clear message
Locked History Actions

Android/Activity

Activty

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

protected void onRestart () Since: API Level 1

Called after onStop() when the current activity is being re-displayed to the user (the user has navigated back to it). It will be followed by onStart() and then onResume().

For activities that are using raw Cursor objects (instead of creating them through managedQuery(android.net.Uri, String[], String, String[], String), this is usually the place where the cursor should be requeried (because you had deactivated it in onStop().

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

  • onStop()
  • onStart()
  • onResume()

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()