I have a question regarding the logic of the life cycle of activities:
When I learned Android, setting up an Activity was always done in the onCreate() function. Now when I resume my Activity, there might be stuff to be done in the onResume() that has already been done in onCreate(). But then, why would we not just put all the stuff into onResume()?
why would we not just put all the stuff into onResume()?
Well onCreate() is called when your Activity is created and you need to initialize some very important things of your application like your main layout!.
Because some staff you don't want to do all the time activity resumed (like findViewById), It can be done once.
Activity can be resumed many times (with dialogs, for example). Do not waste resources.
OnCreate Method start when you open the activty for first time or open activity after kill app so you should locate all stuffs you need to initialize for once like setContentView or some variables.
For example when you run your app with IDE , on create method will call
But if after creating activity press home button activity will pause
And then open the app with recent app to call onresume() method
For better realize initialize all activity method with Log.i(tag,text)
Related
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
My team wrote a complex LBS/chat App, there is a MainActivity as main displaying activity.
When this MainActivity started, "onCreate" is called the first time.
when I switch to other several Apps and go back to my App's MainActivity, "onCreate" is called second time.
But "onDestroy" of MainActivity has never been called.
Some pointers(Singleton/Global, not belong to any activity) will be "auto" set to null out of my control, it causes my app problem.
Does some module/class hold a reference to MainActivity or some other reasons?
I need help -_-!
Thanks to all guys who can give me some tips.
Thanks!
OnCreate is call only once in activity circle life.
If your application was in background and return to foreground, the only reason that on create call again is because OS kill this activity.
you can save data with onSaveInstanceState().
when you start activity this functions be called: onCreate() , onStart() , onResume() and when you going to another activity, onPause() called.
If you want to call onDestroy() to your app you must call this.finish(); but, when you call this.finish() you must handle onBackPress() in your app.
I hope this useful to you
I have Activity A and I am calling Activity B from Activity A using setResultForActivity.
Now in Activity B when I press Done button I am firing finish() and it returns to
Activity A and it return down to onActivityResult. Now the issue is after when I fired finish() in Activity B , Activity A's onCreate doesn't get called and thats why
some of the custom listeners in my ListView isn't working , it seems that they are not bind.
so the whole activity respond pretty weirdly , can anyone has solution to this ?
Why a fourth answer? Because in my view, the others aren't fully correct.
Truth is, Activity A could have been destroyed in the meantime, or not. This depends on whether Android needs memory or not. So it is possible that Activity A´s onCreate() is called (along with the other lifecycle callbacks), or not. In the latter case, onActivityResult() is called before onResume().
While for configuration changes, the most efficient way to preserve the Activity's state is via nonConfigurationState, if you want to prepare for a restart of your Activity after it has been destroyed, you can use the InstanceState mechanism, because while Android destroys your Activity A, it will keep its intent and saved instance state to re-crearte it.
This stresses the absolute necessity to place initialization exactly in the callback where it belongs.
To test whether your Activity logic works regardless of whether Android destroyed it or not, you can use the DevTools setting "Development Settings" -> "Immediately destroy activities". The DevTools app is available on AVDs and can also be downloaded from Google Play.
Just place your onCreate() stuff in onResume() of Activity A except setContentView().
Just have a read on Android Activity Lifecycle : http://developer.android.com/training/basics/activity-lifecycle/stopping.html. onCreate() is only called when the activity is first created. You can do your list thing in the onResume() method.
Activity A's onCreate won't get called because the activity has not been destroyed. When an Activity regains focus from another activity, it's onStart and onResume get called, so I would put your bound listeners in those. They will also be called when onCreate is normally called.
After your child activity is finished() it return to execute onActivityResult which is in your case in Activity A. The onCreate method is not supposed and does not get called when killing of you sub-activity, a.k.a Activity B.
Please post some source code for us to work on and I will improve my answer! :)
I have an Android activity we'll call A which has a button and another activity B. When the user clicks the button in Activity A, I'd like to finish A (let both onStop and onDestroy finish running) and then start up the instance of B. When I put a finish() and startActivity() call in the button click listener, the instance of B starts up before the old instance of A finishes. Can someone help me figure out a way to do what I'm looking for?
What you are looking for is not possible and actually is against Android's activity lifecycle implementation.
Correction
It is possible with android:noHistory="true" tag in your manifest, but for what you are trying to do it seems wrong (read the EDIT)... Messing with the activity stack makes a non intuitive application!
Android OS doesn't let you control when activities will be removed from memory (or killed), and therefore all these fancy "Task killers" are so popular (DONT use them, they only make things worse).
When your activity's onStop() is being called, the activity stops completely, and it just hangs in your memory, but that's fine...
If you want to reset the state of activity A, or close the app when exiting activity B, just create a set of rules in both onResume() and onStop(), you can do everything you wish by creating a set of rules in those functions.
for example: have a boolean in activity A that turns true just before calling activity B,call finish() on your activity A's if this boolean is true
I suggest that you take a look at Android's Activity lifecycle diagram, and make sure that everything you do follows the best practice.
EDIT
I saw your comment, it seems like you are trying to create things that are already in your memory, don't recreate them, it's a waste of CPU time, memory, and battery.
Instead, create a static class with a singleton that will hold all your shared data !
I believe you're looking for
onPause()
which is what gets called when the activity is sent to the background. You can do whatever cleanup you want in there. onStop should only be called when a user is exiting out of your program (or launching another one)
onPause is a better place to do this cleanup. See the Saving Persistent State section of the Activity doc.
When an activity's onPause() method is called, it should commit to the backing content provider or file any changes the user has made. This ensures that those changes will be seen by any other activity that is about to run. You will probably want to commit your data even more aggressively at key times during your activity's lifecycle: for example before starting a new activity, before finishing your own activity, when the user switches between input fields, etc.
While I'm not definite that your cleanup is for user changes, the bold sentence above implies that onPause will complete before the next Activity is created. Of course that probably implies that you'll have to move some setup to onResume...
Alternatively, you could move all your cleanup code to a method, let's just call it cleanup and then just call it before you start activity B. You'll have to put in appropriate guards for your onDestroy cleanup too of course.
override finish() method.
implement cleanUp() method.
create boolean isClean=false in the activity
in cleanUp() write your clean up code.
call cleanUp() in your finish()
check for isCleaned in finish() or in cleanUp() if its true then ignore the clean
now before you start B , call cleanUp() and set isCleand=true
after you call B , call finish()
Start activity A
from inside A startService(c) and finsh A
from inside the service , start Activity B
In my app I need to call code whenever the app closes (pauses) and then run code again whenever the app starts again (resumes). For example, I call a web service that syncs the data whenever the app starts or exits. This is easy in iOS because of the central app resume and suspend methods.
I understand the OnPause and OnResume in the Activity, however, is there a central way to handle this? The user could leave the app on Activity3 and come back later, or be in another screen, etc. I'd hate to have to have the same code in every Activity's OnPause and OnResume to handle the "app" startup and shutdown code routines.
Any suggestions?
Thank you.
You could make a common Activity which just handles onResume() and onPause() in a certain way and then make every Activity extend from that one instead of Activity directly.
I would consider subclassing Activty, have each of your activities extend that and just do what you need to when any of them pause or resume.
You could use some static members in an Application class that your new activity base uses to track state or store whatever you need.
Also, Application class has an onCreate() method which will run each time the application is started. There is no pause or resume for Application, however.
Creating a main Activity that is extended by all your other activities and override the onPause and onResume.
But the problem you will face if you want to extend another Activity class such as a ListActivity.
Another approach is to create a new class that extends application and override its onCreate and create a static method that acts as onPause and manually call it by each of your activities