well here's a weird one that's been making my eyes cross for a few hours.
please be patient, much of this is explanation.
i have an Activity. its launchMode is singleTop. i've got log statements in it so i can see which of the various lifecycle routines are being executed.
i start said Activity and it goes thru
onCreate()
onStart()
onResume()
the Activity is passed an Intent and the Intent is picked up by something that runs after onResume()
i think the following happen because my Activity gets pushed to the background because of an incoming cell phone call.
onPause() <---- since i HAVE NOT pressed Back indicating the activity is over this SAVES STATE
while i'm out of the app running my Activity, the OS needs more resources and that, i think, explains why this is called:
onStop()
then i navigate back to my app and i see this runs:
onNewIntent()
ok. i think i understand this - it runs because that's how the OS communicates with an Activity defined as singleTop after its already been started. am i right?
then i see
onRestart()
onStart()
onResume()
which is all standard stuff
then i hit Home, prior to running something that will consume a lot of resources and force the OS to kill my app
i see
onPause() <- saves state
onStop()
when i navigate back i see
onCreate()
onStart()
onResume()
which makes sense because my Activity was killed and i've navigated back. onCreate() is how you start something that wasn't already running (since it was killed). onStart() and on Resume() are part of the normal lifecycle.
so i hit HOME again, the OS kills my app
onPause() <- saves state
onStop()
and i go back to it
onCreate()
onStart()
onResume()
HERE'S WHERE THINGS GET ODD:
i'm done with my Activity and i hit Back to leave it
onPause() <------- i detect Back was pressed so i DO NOT save state and delete previously saved state
onCreate() <------- say what?
i expected to see the standard stuff that comes after onPause() like onStop() and onDestroy() but for some reason that doesn't happen.
the OS runs my onCreate() again but since the Activity is over and i tried leaving by hitting the Back key this was all detected in onPause() and NO STATE WAS SAVED.
onCreate() thinks its running all over again. but there is no Intent data and
since the Intent delivered in the first onCreate() is gone and i've deleted all saved state onCreate() quickly succumbs to a NullPointerException
what on earth is going on? more importantly, how do i make my Activity die and stay dead?
EDIT 1:
some new information just came to light.
in the problematic onPause()/onCreate() sequence i describe above i logged the value of isFinished(). it shows
onPause() - isFinishing() is true. good that is what i expect.
onCreate() - isFinishing() is false!?
so the OS really is truly staring my Activity again. but why?
Related
I am new to Android, but after studying the Activity Lifecycle, I understood that if I minimise the app, it should call onPause(), and while I reopen it should call onResume(). But, in my case, it calls onCreate() first and then onResume(). This is causing my widgets and other variables to enter wrong state.
My app only has an activity.
Why is the onCreate() method being invoked before onResume()?
It is possible that the app process is killed by the system either from onPause() or onStop() to create room in memory for other apps in the foreground, in which case when you reopen the activity, it will be created. In that case onCreate() -> onStart() -> onResume() is the expected sequence. When the activity is no longer visible, onStop() will be called, so when you navigate back to the activity onStart() -> onResume() is what takes place.
More on activity lifecycle here.
Additionally, since Android 10 there are some restriction of apps running from background. And "an app running a foreground service is considered to be running in the background" which is what you are describing. That may be why it is destroyed and app lifecycle starts from onCreate()
If I understand the issue correctly, it sounds like you're receiving a new instance of your Activity when you're looking to resume it instead. You can change how Activities are handled by setting their launch mode.
To resume an Activity if it already exists you could change its launchMode value in your Manifest to something like this:
<activity
android:name=".MySingleInstanceActivity"
android:launchMode="singleTask" />
There are several launch modes available and there may be one that's better suited for your project. You can read more about tasks and launch modes at https://developer.android.com/guide/components/activities/tasks-and-back-stack#TaskLaunchModes.
Please, help me understand activity lyfecycle more deeply. http://developer.android.com/guide/components/activities.html#ImplementingLifecycleCallbacks
They say:
The foreground lifetime of an activity happens between the call to
onResume() and the call to onPause().
Does this mean, that activity becomes resumed in some moment after onResume() is called, or after onResume() completely finished it's work?
Similar question about visible state and onStart.
And if the second is right (method completely finished it's work), then super.method() or overriden by me in activity class?
#Override
protected void onResume() {
super.onResume();
// is it now "resumed" after super.onResume()?
}
"The foreground lifetime of an activity" referes to the time it is directly being shown to the user. It also implies at the moment its process has maximun priority on the Android process priority ladder. You should read this http://developer.android.com/guide/components/processes-and-threads.html
Furthermore, onResume(), onPause()... are just hooks where you should insert code that needs to be executed on that specific moment of the activity lifcycle.
The foreground lifetime of an activity happens between the call to onResume() and the call to onPause().
Does this mean, that activity becomes resumed in some moment after onResume() is called, or after onResume() completely finished it's work?
Technically speaking, the Activity is in a state of being resumed before onResume() is called but the option for you to override the onResume() method allows you to fine-tune what needs to be done before the Activity enters the 'running' state. In other words, from the point of view of the OS, the Activity is resumed, then onResume() is called and, finally, from the point of view of your own individual app, resuming the Activity is complete when onResume() is complete and the Activity is running.
Similar question about visible state and onStart. And if the second is right (method completely finished it's work), then super.method() or overriden by me in activity class?
Again, the same logic applies - the OS goes through what it needs to do to start the Activity then calls onStart() for you to customise the starting stage of your Activity. The OS considers the Activity to have started before it calls onStart() but from your app's perspective, it hasn't completely become started until any code you have in your overridden onStart() method.
After the onPause() getting called, there are no calls to onStop() nor onDestroy(). The application is still running, and pid is the same (if I check it using "ps" Linux command). When the activity gets back on top, there are no calls to onCreate() nor onResume(). The app appears to be hanging. And after a while messages in the log appear saying "Activity destroy timeout for ActivityRecord". Oh, and it does not all the time. I can run it without getting stuck 2-3 times (with onStop() and onDestroy() called after onPause(), and the new activity getting created when it's time to get on top). But eventually I get the situation described in the title.
Any idea how this could happen? Many thanks :)
Well, the app was actually hanging in the state "after onPause() called and complete", waiting for a (not properly initialized) mutex while trying to handle an external event. The big part of the apps data was destroyed during onPause(), including that mutex.
Can anyone give me an example that uses onResume() in Android?
Also, if I want to restart the activity at the end of the execution of another, which method is executed—onCreate() or onResume()?
And if I want to update data, how do I put it in onResume()?
Any Activity that restarts has its onResume() method executed first.
To use this method, do this:
#Override
public void onResume(){
super.onResume();
// put your code here...
}
Restarting the app will call OnCreate().
Continuing the app when it is paused will call OnResume(). From the official docs at https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle here's a diagram of the activity lifecycle.
The best way to understand would be to have all the LifeCycle methods overridden in your activity and placing a breakpoint(if checking in emulator) or a Log in each one of them. You'll get to know which one gets called when.
Just as an spoiler, onCreate() gets called first, then if you paused the activity by either going to home screen or by launching another activity, onPause() gets called. If the OS destroys the activity in the meantime, onDestroy() gets called. If you resume the app and the app already got destroyed, onCreate() will get called, or else onResume() will get called.
Edit: I forgot about onStop(), it gets called before onDestroy().
Do the exercise I mentioned and you'll be having a better understanding.
When you open the app it will go through below states:
onCreate() –> onStart() –> onResume()
When you press the back button and exit the app
onPaused() — > onStop() –> onDestory()
When you press the home button
onPaused() –> onStop()
After pressing the home button, again when you open the app from a recent task list
onRestart() –> onStart() –> onResume()
After dismissing the dialog or back button from the dialog
onResume()
If a phone is ringing and user is using the app
onPause() –> onResume()
After the call ends
onResume()
When your phone screen is off
onPaused() –> onStop()
When your phone screen is turned back on
onRestart() –> onStart() –> onResume()
Happy Coding...#Ambilpura
Most of the previous answers do a good job explaining how, why, and when to use onResume() but I would like to add something about re-creating your Activity.
I want to know if I want to restart the activity at the end of exectuion of an other what method is executed onCreate() or onResume()
The answer is onCreate() However, when deciding to actually re-create it, you should ask yourself how much of the Activity needs to be re-created. If it is data in an adapter, say for a list, then you can call notifyDataChanged() on the adapter to repopulate the adapter and not have to redraw everything.
Also, if you just need to update certain views but not all then it may be more efficient to call invalidate() on the view(s) that need updated. This will only redraw those views and possibly allow your application to run more smoothly. I hope this can help you.
onResume() is one of the methods called throughout the activity lifecycle. onResume() is the counterpart to onPause() which is called anytime an activity is hidden from view, e.g. if you start a new activity that hides it. onResume() is called when the activity that was hidden comes back to view on the screen.
You're question asks abou what method is used to restart an activity. onCreate() is called when the activity is first created. In practice, most activities persist in the background through a series of onPause() and onResume() calls. An activity is only really "restarted" by onRestart() if it is first fully stopped by calling onStop() and then brought back to life. Thus if you are not actually stopping activities with onStop() it is most likley you will be using onResume().
Read the android doc in the above link to get a better understanding of the relationship between the different lifestyle methods. Regardless of which lifecycle method you end up using the general format is the same. You must override the standard method and include your code, i.e. what you want the activity to do at that point, in the commented section.
#Override
public void onResume(){
//will be executed onResume
}
KOTLIN
Any Activity that restarts has its onResume() method executed first.
To use this method, do this:
override fun onResume() {
super.onResume()
// your code here
}
Re-review the Android Activity Lifecycle reference. There is a nice picture, and the table showing what methods get called. reference Link google
https://developer.android.com/reference/android/app/Activity.html
After an activity started, restarted (onRestart() happens before onStart()), or paused (onPause()), onResume() called. When the activity is in the state of onResume(), the activity is ready to be used by the app user.
I have studied the activity lifecycle a little bit, and here's my understanding of this topic:
If you want to restart the activity (A) at the end of the execution of another, there could be a few different cases.
The other activity (B) has been paused and/or stopped or destroyed, and the activity A possibly had been paused (onPause()), in this case, activity A will call onResume()
The activity B has been paused and/or stopped or destroyed, the activity A possibly had been stopped (onStop()) due to memory thing, in this case, activity A will call onRestart() first, onStart() second, then onResume()
The activity B has been paused and/or stopped or destroyed, the activity A has been destroyed, the programmer can call onStart() manually to start the activity first, then onResume() because when an activity is in the destroyed status the activity has not started, and this happens before the activity being completely removed. If the activity is removed, the activity needs to be created again.
Manually calling onStart() I think it's because if the activity not started and it is created, onStart() will be called after onCreate().
If you want to update data, make a data update function and put the function inside the onResume(). Or put a loadData function inside onResume()
It's better to understand the lifecycle with the help of the Activity lifecycle diagram.
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