Android development: Lifecycle of Activity at screen transition

Introduction

Activity’s life cycle is the basis of developing basic Android applications.If you do not know the basic life cycle, you can not implement it in the first place, but you need to understand the life cycle a little more when you want to control more finely (eg change the behavior only when pressing the home key) I will come.So this time about the life cycle of Activity,What methods are called with various screen transition patterns.

About the life cycle of Activity

Basic life cycle method

Let me first review the life cycle of Activity.It is like the following.

Method name Timing called
onCreate () When Activity is first created
onStart () When Activity is started
onResume () When Activity is displayed
onRestart () When Activity is restarted
onPause () When another activity is displayed
onStop () Activity is no longer displayed
onDestroy () Just before Activity is released from memory

This part is pretty pretty, so Android application developers know it is natural.

Other life cycle methods

There are several other life cycle methods. Although the usage frequency is lower than the methods listed above, it is a convenient method to know.

Method name Timing called
onApplyThemeResource () When a theme is applied to Activity
onPostCreate () When the start of Activity is completed
onPostResume () When the resume of Activity is completed
onActivityResult () When the request result is returned to another Activity
onNewIntent () When a new Intent is issued and called (single top only)
onSaveInstanceState () When to save the state of Activity
onRestoreInstanceState () Called by Intent, when restoring the state of Activity
onUserLeaveHint () Activity leaves user operation

As you can supplement the basic life cycle method, you can see that it can be handled at various timings. If you use these methods successfully, you will be able to control various details.

Confirm method called by screen transition pattern

I tried logging when all of the above methods were called and checked the methods and their order called when manipulating several screen transition patterns.

Activate Activity

It is the case when it is displayed for the first time after the application is launched.

onApplyThemeResource () has been called before onCreate ().

Press back key while Activity is displayed

I closed the Activity by pressing back key while displaying Activity.

Since it does not return to Activity anymore, it is called until onDestroy () and it is completely destroyed.

Press the home key while Activity is displayed

While displaying Activity, I pressed the home key and returned to the home screen.

onUserLeaveHint () is called. With this you can implement the process when pressing the home key. But since onUserLeaveHint () has other timing, it is necessary to be careful. In addition, onSaveInstanceState () saves the state of Activity.

Reopen suspended Activity from Home screen

When you return to the home screen while launching the application, Activity will be in the stopped state. I opened the application again in that state.

Activity is already generated, so onCreate () etc is not called. onRestart () is called before onStart ().

Transit to another Activity

I placed a button to transition from Activity A to Activity B and made it transition to Activity B after Activity A was displayed.

OnUserLeaveHint () is called in Activity A which becomes invisible. So, when considering the control of the home button, you have to be careful that onUserLeaveHint () will also be executed at the transition to another screen. Activity A ‘s onSaveInstanceState () and onStop () are called after the lifecycle until Activity B is displayed. Let’s process onStop () after Activity A is hidden (eg destruction of Bitmap shown in ImageView).

Return from other Activity

With Activity A → Activity B transition, I pressed the back key and returned to Activity A.

ActivityB ‘s onPause () is called first, then Activity A restarts. A method to stop and destroy ActivityB which became unnecessary after the display of Activity A is ended is called.

Sleep state while displaying Activity

Screen OFF (sleep state) was set while Activity was being displayed.

The same method as when transitioning to another screen is called.

Display activity again from sleep

I made it sleep in Activity display state and then displayed it again.

It is the life cycle at normal restart. By the way, these methods are called when the screen turns on (when the lock screen is displayed).

Display Activity after leaving it for a long time

If you leave it for a long time with Activity stopped, or when another application requests memory, it will destroy the application that is stopped once and will recreate Activity the next time it is called.This state can be reproduced by checking “Do not keep the setting for> setting> developer option> activity” from Android 4.0, so I tried it with this time this time.

The normal lifecycle method when generating Activity appears to be called but onRestoreInstanceState () is mixed. This is called to restore the state saved with onSaveInstanceState () when Activity stops.

Summary

Android’s screen transition patterns are quite different. You can start other applications while launching the application, or you can freely cooperate between applications using Intent. Depending on the Intent type, there are various screen transitions in the application conceivable. for that reason, It is very important to understand what kind of life cycle methods are called in what order in screen transition.First of all, it is a good idea to put only the implementation that transitions to the screen according to the wire frame, to take the log of the life cycle method and to include a process to consider what kind of processing should be implemented at what timing.

Leave a Reply