First of all, I've read the great explanation of how the activities start, suspend, resume and stop. It's fine but I have another problem.
The Activity lifecycle diagram in the Android reference tells that if I call another activity, onPause() will be called for the calling activity, and later, when the other activity is over--the caller will resume via onResume().
So if the first activity is Main and the other one is Other, the cycle would look like this (pseudocode):
Main.onCreate()
Main.onStart()
Main.onResume()
// Main is running... Then, the user clicks a button and Other comes in front.
Main.onPause()
Other.onCreate()
// Other's lifecycle goes here... Finally, the user returns back.
Main.onResume()
// Main is running again.
This is what the diagram tells. But my Main gets onStart() first, then onResume().
Why is that? Do I misunderstand something?
That is happening because your Main activity is totally disappearing from view, which triggers onStop, which triggers OnStart when you resume. If you only partially hid the view from your Main, you would only get onResume.
If you look at the diagram, between onPause and onStop, there is this "the activity is no longer visible"... that's what you are encountering.
For quick reference, the activity lifecycle graphic:
One reason of your onStart() is getting called in for main application is that your Main activity is stopping. That is its onStop() is being called. In this scenario firstly onStart() and then onResume() will be called.
Related
I have tried to research exactly when the onDestroy method is called for an activity, but I've read some confusing and conflicting information. In general, my question is: under what circumstances is the onDestroy method actually called on an activity? More specifically, if I have two activities, activity A and activity B, if activity A is running and I create an intent and switch to activity B, is activity A only stopped, or is it destroyed?
Like stated in the official documentation:
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.
In your example the Activity A is stopped and could be destroyed by the System
Note per documentation link above:
…do not count on [onDestroy()] being called as a place for saving data … [see] either onPause() or onSaveInstanceState(Bundle).
onDestroy() is called whenever:
The user takes out the activity from the "recent apps" screen.
The user takes out the activity from the "recent apps" screen.
onStop() is called whenever:
The user leaves the current activity.
So in your example, when the user launches Activity B, Activity A called onStop().
EDIT:
The onDestroy() method is not always being called, according to the documentation. onStop() is always called beginning with Honeycomb, so move code you explicitly need to do before the activity stops to there.
Starting with Honeycomb, an application is not in the killable state until its onStop() has returned.
https://developer.android.com/reference/android/app/Activity#ActivityLifecycle
Hope this helped :D
I looked on another thread on stack overflow (src: Difference between onCreate() and onStart()?)
That thread described the onStart() method as "Called when the activity is becoming visible to the user". However in that same answer and in many overrides of the oncreate method, i see setContentView called in onCreate. Wont that make the screen visible then? Therefore in that situation(where setContentView is called in onCreate), is onStart() called after the screen becomes visible to the user but before the user can interact with it?
The chances of onStart() can be called multiple times.
onCreate() : Called when the activity is first created.
onStart() : Called when the activity is becoming visible to the user.
Now look into the graph given to Difference between onCreate() and onStart()? post. onStart() can be called multiple times, in case if process is not killed (if activity has been called again.)
So if you set view at onStart(), you will need to initialize view into onStart() or later (i.e. onResume() ). This will be a repetitive process. Is not it a bad practice to initialize view again and again?
Hope I am clear here.
onCreate called when activity is first created.
onStart called when activity is becoming visible to the user.
and also
onResume called when activity will start interacting with the user.
onPause called when activity is not visible to the user.
onStop called when activity is no longer visible to the user.
onRestart called after your activity is stopped, prior to start.
onDestroy called before the activity is destroyed.
For difference between onCreate and onStart see this link
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 an activity that starts another then if the user cancels from the second I call finish() and return to the first.
Question: Is there a method that gets called when I get returned to the first activity?
Thanks for any help
Yes, onResume() is called.
Check the :Activity Lifecycle
See the lifecycle:
Yes there is onStop() and onStart(), for more details see:
http://developer.android.com/reference/android/app/Activity.html
The original application will receive whatever callbacks are on the activity life cycle flowchart that are between the application's current state and "Activity Shut Down" state. onDestroy() is the only one guaranteed to happen.
If the finished application was on the top of the stack then the Activity that replaces it on the top of the stack will receive at least onResume(). If it was completely invisible (i.e. it had received onStop(), then it will also receive onRestart() and onStart() before onResume().
If the finished Application was not on top of the stack then no other Application is notified.
Here is the scenario:
I have two Activities. Lets name them Activity A and Activity B.
Say Activity A is open. Now, when I go and open Activity B, Activity A is closed because the onStop() method is called.
Now, when I flip back to Activity A, the onCreate() method is called, but I want the onRestart() method called instead. How do I do this?
You cannot influence the livecycle of your app like that. There should be no reason to rely on onRestart(). If you use onStart() it will always be called no matter if the Android OS killed the app process in the background.
Check out this doc for further information:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Damn beat me to it but here goes anyway
According to the Activity Lifecycle onCreate() is called again if the Activity was removed from memory because the OS deemed that another app needed the memory. In this case, you can't ensure that onRestart() will always be called for your Activity.
Like already stated you must find a different way of achieving your goal by using the other Lifecycle methods such as onStart() or onResume
I'm not sure if it fits your needs, I had to do an update service that starts the first time I open ActivityA (main Activity) and stops when exiting from ActivityA (not returning back from ActivityB),
I've placed the "start code" in onCreate() when savedInstanceState is null and the "stop code" in onDestroy() if isFinishing() is true