开发者

What methods are invoked in the Activity Lifecycle in the following cases:

Let's say I have a Hello World single Activity application. I start this application.

What methods are invoked in each case:

What methods are invoked once the user starts the application again via the app icon (assuming the OS hasn't had a "other apps need memory condition"):

  • Home button was pressed: ?

    Back button was pressed: ?

    Phone call was received: ?

Thanks all.

Edit: Extra Credit: How can the user invoke onPause without invoking onStop?


both pressing home button and receiving a call don't remove the activity from the task's stack, and will be available when you re-enter the app => onPause() => onStop().

as the activity lifecycle diagram shows, re-entering the app calls => onRestart() => onStart() => onResume()

pressing the back button instead kills the activity => onPause() => onStop() => onDestroy()

re-entering the app in this case calls the classics => onCreate() => onStart() => onResume()

EDIT

from http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.


There can be several scenarios

  1. Opening the app from the app icon. following methods are called

    onCreate()-->onStart()-->onResume()

  2. When user presses the home button

    onPause()-->onStop()

  3. When user returns to the app from the Activity Stack

    onRestart()-->onStart()--> onResume()

  4. When the app is running and user presses the power button

    onPause()-->onStop()

  5. When user unlocks the phone

    onRestart()-->onStart()--> onResume()

  6. When user gets an incoming call while you are in the app

    onPause()

  7. When user returns to the app after disconnecting the phone call

    onResume()

  8. When user presses the back button from the app

    onPause()-->onStop()-->onDestroy()

  9. And when the user presses the home button and from the activity stack user swipes the app.onDestroy() method may or may not be called depending upon the OS contains the context of the Activity or not according to memory requirements


Well see, while a sequence of events may occur with your hello world program, the same sequence may not occur in say a video game, because Android will probably Destroy it for taking up too much resources.

The best way I have found to see the lifecycle for my app is to override all the methods (onStart, onRestart,..., including the onSaveInstance and onRestoreInstance) and insert log statements in each one. Like so:

@Override
public void onDestroy() {
    // Call the super class 
    super.onDestroy();
    // Log the action
    Log.d("Debug", "onDestroy() has been called!");
}

Now I can go to logcat and see what events took place.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜