Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detailed android activity lifecycle (onAttachedToWindow())

I'm interested in android activity lifecycle and I would like to get more detailed description/documentation/reference than widely available basic (onCreate->onStart->onResume) one.

My need comes from realizing that starting new activity (Theme.Dialog styled) from onAttachedToWindow() greatly improves response time if comparing to starting it from onCreate(). I wonder how this onAttachedToWindow() fits into whole android activity lifecycle. Official API ref description "Called when the window has been attached to the window manager" doesn't help a lot.

like image 973
Ralkie Avatar asked Jun 14 '10 14:06

Ralkie


People also ask

What is onSaveInstanceState () and onRestoreInstanceState () in activity?

The onSaveInstanceState() method allows you to add key/value pairs to the outState of the app. Then the onRestoreInstanceState() method will allow you to retrieve the value and set it back to the variable from which it was originally collected.

What is the life cycle of Android activity?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() .

What is the difference between onCreate () and onStart ()?

onCreate() is called when the when the activity is first created. onStart() is called when the activity is becoming visible to the user.

Why do we need to call setContentView () in onCreate () of activity class?

onCreate() method calls the setContentView() method to set the view corresponding to the activity. By default in any android application, setContentView point to activity_main. xml file, which is the layout file corresponding to MainActivity.


1 Answers

My guess for why that feels more responsive, off the top of my head: I think that if you start Activity B from activity A's onCreate(), Activity A isn't drawn before activity B starts, which may take another second or two (making the app feel less responsive), where if you start activity B in Activity A's onAttachedToWindow(), A is started and rendered before B is kicked off, so the user doesn't have to sit for a second with a blank screen or the pre-A activity before seeing a reaction to their action.

like image 146
Yoni Samlan Avatar answered Nov 07 '22 05:11

Yoni Samlan