How to use onResume()?
Asked Answered
C

9

97

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()?

Corpulence answered 27/3, 2013 at 12:22 Comment(4)
onResume() is called whenever you navigate back to the activity from a call or something else. You can override the onResume method similarly as onCreate() and perform the task.Daleth
This may help you understand the lifecycle of and Android app more.Eyrie
The sequence in which these methods are called is explained in the Android developer documentation: developer.android.com/reference/android/app/…Offertory
make sure to select an answer. welcome to SO.Eveleen
P
151

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...

}
Prosperity answered 27/3, 2013 at 12:31 Comment(0)
B
87

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 Android activity lifecycle, from https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfkX7XlaFfeX79QWV2kbmfpXC2jaRA/images/activity_lifecycle.png on https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Bey answered 27/3, 2013 at 12:26 Comment(5)
Use the hyper link to provide links.Rico
the link is giving me prob sometimes. i dont know whyBey
I don't recommend to manually call any of the methods that are part of the Activity lifecycle :), that can cause a lot of issues.Habitude
@Cata, I assumed Viswanath meant that the methods will be called, and I've updated the answer accordingly.Espinal
I wanted to note that onResume is called before onCreate nowadays!Bathometer
P
29

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.

Precessional answered 27/3, 2013 at 12:37 Comment(2)
I've been looking for such a detailed explanation for a while, this really helped me to create the perfect app activity cycle. Thank you.Orosco
you forgot onStart()Oregano
A
14

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

Asceticism answered 23/3, 2021 at 18:34 Comment(1)
When you press minimize button onPaused()Simferopol
K
9

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.

Karlene answered 27/3, 2013 at 12:50 Comment(0)
G
6

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
}
Gustafson answered 27/3, 2013 at 12:37 Comment(3)
Is onResume() called when we click on an Item in the view?Canonry
Clicking an item does not necessarily call onResume. The onResume method will be called if that view is either hidden and restored or on initial load before it's fully created.Gustafson
show error did not call through to super.onResume()Testee
H
5

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
    }
Horse answered 20/5, 2020 at 9:53 Comment(0)
E
3

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

Eveleen answered 27/3, 2013 at 12:30 Comment(0)
S
1

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.

  1. 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()

  2. 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()

  3. 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.

Symbolics answered 2/12, 2019 at 1:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.