When android actually renders and displays the activity? - android

I am trying to understand on which stage android OS is rendering layouts, in order to be able to access views in my ListView. Thanks

The layouts are created in onCreate():
The entire lifetime of an activity happens between the first call to
onCreate(Bundle) through to a single final call to onDestroy().
The Activity is visible from onStart():
The visible lifetime of an activity happens between a call to
onStart() until a corresponding call to onStop(). During this time the
user can see the activity on-screen
Source: the documentation.

Related

Initialize views of activity in android

Is it necessary to initialize all the views of activity in onCreate() only. Can you tell me best initializations of views of activity.
Thanks
No need to initialize any view if you do not modify it.
you can initialize in any Activity lifecycle as your wish(before accessing).
But is is said as best practice to initialize it in onCreate().
Why:
if you see the lifecycle OnCreate is called when you app page is not shown. Like onStart called when app is partially visible & onResume is called when it is fully visible. So, mostly we want everything ready before seeing it. So that is one reason.
Another is findViewById is bit more expensive. So, we don't want to see that when app is visible.
OnStart & onResume may call multiple time when you go another page. So, it's preferable to initialize everything only once , than multiple time.
So, choice is yours now.
OnCreate() : This method is called once when activity is to be created. That is why all the gobal and static content should go there. Example - This may include your shared preferences, databases initialisations.
OnStart() : This method is called when you see your activity over screen. It is foreground method. OnStart() ends with OnStop(). Example : Let us assume A and B activity, A activity has been created and currently in onStart() method is being called. When one switches to B activity then A's OnStop() method will be called and B activity will be created. Thus OnStart() and OnStop() methods are called when you switches onto activities.
So according to your question initialisation is done once so it should be done in OnCreate() method if it is done in OnStart() then initialisation will takes place every time when you switch between activities.
Source : Difference between onCreate() and onStart()?
Please take a look over here this will clear your all faults regarding life cycle Activity | Android Developer

What we should do in onStart, OnResume, OnPause

Hi I have gone though activity lifecyle on many threads, but I could not find what we should do in onStart, onResume, onPause method of the activity.
In the onStart() method, you add code that's relevant at the beginning of the activity.
Let's say, you have an app that reads the temperature of the device's battery. You'll want to have an initial value, so as to show the user.
So in the onStart(), you'd add code that goes ahead and fetches the information you'd need, and displays it for the user, before your timer (for example) goes and reads the information a minute later.
The onPause() method is called before the application goes in to the background.
To stay with our example, in the onPause() method, you'd save the last recorded temperature to the device; so you can show a comparison when the user next opens the app.
The onResume() method is called when the application is brought back to the foreground (i.e.: you've gone to the task manager, and tapped on your app to show it again).
Again, staying with the going example; in the onResume() method, you'd go ahead, read your saved data, load fresh data, and show a comparison of the two in the application.
Then, when your timer ticks next, only fresh data will be shown.
Your question is a bit vague, so answer might not be super specific..
I would say there are no strict "rules" around what we should do in corresponding activity lifecycle methods.
In fact, you can do nothing there (just make sure you call super method if you decided to override those). I.e. your custom activity might not even override these methods - it will work just fine.
onStart, onResume and onPause methods are just hints to you about activity lifecycle change, so you can react accordingly, i.e. start/stop specific to your activity operations at the appropriate time.
For instance, when onResume is called it means that activity became fully visible to the user, so you might want to start some animation (if necessary)
Again, you are not obligated to put any code in there.
Usually most of the operations are performed within oncreate and onresume.
However for your info let me brief it out,
Onstart- this is called after Oncreate, once activity is visible to the user, if you want to perform some operations before the visibility do it in Oncreate, because most of codes should be operated before user views the activity.
OnResume-Be cautious on Onresume is it is quite tricky it will be called whenever you activity is brought to foreground.
Onpause-Called before Onresume, codes wont be executed here, so strictly avoid adding codes in Onpause instead add inside Onresume.
Hope it helps,

What is the performance impact of onCreate() and onStart() on activity startup

Whenever a new Activity is created onCreate is called. Followed by onStart. And onStart is called again when activity is back on to the screen. I know this fundamentals. But, what is the actual difference between when you launch the activity.
I mean to say, when you click on some object on current activity, you start a new activity by startActivity() method with an intent of new activity. Now onCreate() will be called of the second activity and then the onStart(). When will be the activity visible to the user? After onCreate or after onStart? If it is visible after onCreate and before onStart, and I do some of the operations in onStart(), then it will reduce the lag between the user clicking on an object and the screen popping up on screen.
If I move some of the data bindings to onStart will it interfere with the default Activity transitions on lollipop and above (I am not sure about this)?
Is it a good idea to move some of the code to onStart to reduce a lag between click and new activity shown on user's screen? If yes, what kind of code can be safely moved to onStart? Like data bindings, database queries, etc. ?
Any guidance will be much appreciated.

Android life cycle: When each step should happen

This may be a bit of a stupid question but when is the best part of the Android life cycle to implement each step? The flow of my game is as follows:
Before onCreate: Data that is stored in a JSON file is parsed into String Lists when the game starts
onCreate: Basic UI is generated
onStart/onResume: Game starts: item selected at random from the lists, user selects corresponding item to proceed
If the user is correct, another item is selected from the lists. Occurs 10 times
After 10 items, game ends and score is displayed to user
Would this be considered good practice? I'm a bit confused about the life cycle steps
This might help with understanding the lifecycle of an Android app more. The following is quoted from that site:
As mentioned in the previous section, the lifecycle of an activity has 4 states and 3
lifetime periods. If you want to monitor and adding your own code
logics to an activity, you can use the following 7 basic callback
methods provided by the android.app.Activity class:
onCreate() - Called when the activity is first created. This is where
you should do all of your normal static set up: create views, bind
data to lists, etc. This method also provides you with a Bundle
containing the activity's previously frozen state, if there was one.
onCreate() is always followed by onStart().
onRestart() - Called after
your activity has been stopped and prior to it being started again.
onRestart() is always followed by onStart().
onStart() - Called when
the activity is becoming visible to the user. onStart() is followed by
onResume() if the activity comes to the foreground, or onStop() if it
becomes hidden.
onResume() - Called when the activity will start
interacting with the user. At this point your activity is at the top
of the activity stack, with user input going to it. onResume() is
always followed by onPause().
onPause() - Called when the system is
about to start resuming a previous activity. This is typically used to
commit unsaved changes to persistent data, stop animations and other
things that may be consuming CPU, etc. Implementations of this method
must be very quick because the next activity will not be resumed until
this method returns. onPause() is followed by either onResume() if the
activity returns back to the front, or onStop() if it becomes
invisible to the user.
onStop() - Called when the activity is no
longer visible to the user, because another activity has been resumed
and is covering this one. This may happen either because a new
activity is being started, an existing one is being brought in front
of this one, or this one is being destroyed. onStop() is followed by
either onRestart() if this activity is coming back to interact with
the user, or onDestroy() if this activity is going away.
onDestroy() -
The final call you receive before your activity is destroyed. This can
happen either because the activity is finishing (someone called
finish() on it, or because the system is temporarily destroying this
instance of the activity to save space. You can distinguish between
these two scenarios with the isFinishing() method.
How are you pre-loading data before onCreate? You got no Context object to work with app filesystem before your onCreate called. Here is documentation for Activity lifecycle, as you can see, onCreate is the first method where you can run your code.
I suggest to put data loading in other thread, for example AsyncTask.
So:
Generate basic UI
Start data pre-loading NOT IN MAIN THREAD
Update UI after data loaded

Interactivity Coordination: onStop and onResume

I have two activities. One loads all rows from a database, and the other saves to the database. When I have the second save in onStop and the first repull the data in onResume, they do it out of order (the first resumes and then the second saves). I managed to fix this by putting the saving data in onPause, but why was this happening? Was this the cleanest way to do it?
Doing the save in the first actvity's onPause should be fine.
You've discovered that the foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time, the activity is in front of all other activities on screen and is interacting with the user.
When you start the second activity, onPause is called on the first and then interactive control switches to the second, with onStop on the first to be called somewhat in background.
This improves responsiveness and gets the new activity in front of the user ASAP. Consequently, you should try to keep your onPause implementation as fast and efficient as possible.
See the following Android docs for more details on the lifecycle http://developer.android.com/guide/topics/fundamentals.html, but what you have found should work fine for you.
Some official quote as an add-on:
The Activity documentation states that
onPause() is where you deal with the user leaving your activity.
Most importantly, any changes made by the user should at this point be
committed

Categories

Resources