Android Activity Detailed Lifecycle (onAttachedToWindow ())

I'm interested in the life cycle of android activity, and I would like to get a more detailed description / documentation / link than the widely available basic (onCreate-> onStart-> onResume) one .

My need comes from understanding that the start of a new activity ( Theme.Dialog style) from onAttachedToWindow() significantly improves the response time when compared with onCreate (). I wonder how this onAttachedToWindow() fits into the entire life cycle of android activity. The official description of the API API "Called when the window was attached to the window manager" does not help much.

+7
android android-activity dialog
source share
2 answers

My guess about why this feels more responsive is from my head: I think that if you start Activity B from Activity A onCreate (), activity A will not be drawn before activity B starts, which may take another second or two (which makes the application less responsive), where if you start an action B in Activity A onAttachedToWindow (), A is launched and visualized before B starts, so the user does not need to sit for a second with a white space or pre-A, before than to see the reaction to their action.

+4
source share
  • When the action starts, the onCreate() method is onCreate() . In the onCreate() method, you execute the basic startup logic, which should happen only once for the entire life cycle.

  • After onCreate() is executed, the onCreate() method is onStart() . This method makes the activity visible when the application prepares an action to enter the foreground and becomes interactive.

  • After onStart() is executed, the onStart() method is onResume() . This method is called only when the application is in the foreground , because this is the state in which the application interacts with the user. The application remains in a resumed (or running) state until something succeeds in diverting attention from the application.

  • If another action comes to the forefront , the onPause() method is onPause() . The application is paused if it is still visible . This method only pauses operation operations. It does not destroy. The action remains in a paused state until the activity resumes or becomes completely invisible to the user. 4a. If the user returns to activity, the onResume() method is called again. 4b. An application process can be killed from a paused state if higher priority applications need memory. If the user must return to the application after it is destroyed, the onCreate() method will be called again

  • If the action is no longer visible to the user, the onStop() method is onStop() . When the action is stopped, the activity object is stored in memory and saves all state and information, but is not tied to the window manager. 5a. if the user returns to activity, the onRestart() method is onRestart() , followed by the onStart() method. 5 B. an application process can be killed from a stop state if higher priority applications need memory. If the user must return to the application after it is destroyed, the onCreate() method will be called again.

  • If activity is terminated or killed by the system, the onDestroy() method is onDestroy() . The application does not initially shut down. The system either calls this method because the action ends because someone calls finish() , or because the system temporarily destroys the process containing the operation in order to save space. The system can also call this method when the orientation changes, and then immediately call onCreate() to recreate the process (and the components that it contains) in the new orientation. The onDestroy() method frees up all resources that have not yet been released by earlier methods, such as onStop() .

an entire lifetime of activity occurs between the first call to onCreate() through one final call to onDestroy() .

the visible lifetime of the activity occurs between the onStart() call before the onStop() call.

onResume() between an onResume() call before an onPause() call.

The only method we need to implement is onCreate() . Others are called automatically. But we can implement them ourselves to tell the application what to do during these processes.

https://developer.android.com/guide/components/activities/activity-lifecycle.html#asem

onAttachedToWindow is called when the view is onAttachedToWindow to the window. At this moment he has a Surface and drawing will begin.

https://developer.android.com/reference/android/view/View.html#onAttachedToWindow ()

0
source share

All Articles