Follow-up will continue to update, it is recommended to collect attention

What is an Activity and describe its life cycle

What is this question about?

Having developed Android for years, you should have a very deep understanding of activities. If you only know the answer to the life cycle, it is too shallow. Bonus points if you can answer some activity-related practical tips you’ve used in your projects.

Subdivided down the knowledge point

  1. Overall awareness of activities
  2. The Activity life cycle

How to answer

  1. Activity is one of the four components of Android. Generally, one user interface corresponds to one Activity. The setContentView() method is usually used to set the layout to display. The Activity is a subclass of Context that implements both window.callback and keyevent.callback to handle events that interact with the user of the form.

  2. The Activity has multiple states from creation to destruction, and the corresponding callback methods are fired when moving from one state to another. These callback methods include: onCreate-> onStart-> onResume-> onPause-> onStop->onDestroy. To restore from onStop to onStart, go through onRestart.

    In fact, these methods are pairwise, onCreate and onDestroy; OnStart visible and onStop not visible; OnResume editable (i.e. focus) with onPause;


OnStart is similar to onResume, onPause is similar to onStop, but what’s the difference?

What is this question about

An Activity has different states, and lifecycle methods are purely callbacks to changes in those states. This question tests your understanding of the Activity state.

Subdivided down the knowledge point

  1. How many states are there
  2. Answer the difference between onStart and onResume, onPause and onStop
  3. Bonus points if you can answer how to call life cycle functions in some special cases

How to answer

  1. Running state: After a new Activity is started, it is at the front of the screen, at the top of the stack, and is active in a state that is visible and interactive with the user. Paused state: The state in which an Activity is overwritten by another transparent or Dialog-style Activity. At this point it is still connected to the window manager, the system continues to maintain its internal state, it is still visible, but it has lost focus and therefore cannot interact with the user.

    Stopped State: The Activity is in the Stopped state when it is not visible. It is important to save the current data and UI state while the Activity is in this state, otherwise the current data and UI state will be lost when the Activity exits or shuts down.

    Killed state: The Activity was in the Killed state after it was Killed or before it was started. This is the Activity that has been removed from the Activity stack and needs to be restarted to be displayed and used.

  2. OnStart () and onResume()

    OnStart () is performed when the activity screen is displayed, visible to the user, including an activity on top of it, but not completely overwritten, so the user can see part of the activity but not interact with it.

    OnResume () is executed when the activity can interact with the user, so that the user can get the focus of the activity and interact with the user.

  1. OnPause: The Activity is unfocused but still visible;

    OnStop: The Activity is triggered when the background is not visible (completely blocked by another Activity, or the program is running in the background)

  2. Scene:

    • To lock the screen, call onPause() and onStop().

    • Toast, Dialog, and menu do not cause the Activity to call onPause();

    • When a non-full-screen Activity is in front of it, the following Activity only calls onPause();

    Summary: Dialog does not call onPause() and onStop(), non-full-screen activities do call onPause() and non-full-screen activities do not call onStop(), full-screen activities do call onPause() and onStop().