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.
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
}
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
来源:https://stackoverflow.com/questions/15658687/how-to-use-onresume