How to use onResume()?
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()?
Solution 1:
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...
}
Solution 2:
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.
Solution 3:
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.
Solution 4:
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.
Solution 5:
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