Android onCreate onResume
Asked Answered
L

4

15

I have a problem. When I start for the first time my android application, in the main activity both the onCreate and the onResume are called. but I want to be called only the onCreate.

What can I do?

Lochner answered 30/5, 2011 at 11:27 Comment(2)
remove the onResume() if u don't want.it won't affect.Sextant
Check this answer it may be useful: https://mcmap.net/q/80309/-android-activity-life-cycle-what-are-all-these-methods-forSnowblink
V
42

According to the SDK docs what you are seeing is the intended behavior. Have a look at the flowchart in the docs for Activity - Activity Lifecycle.

Programmatically you can overcome this by keeping an instance member to track whether onResume has been called before - the first time it is called, set the variable and return e.g.

private boolean resumeHasRun = false;

@Override
protected void onResume() {
    super.onResume();
    if (!resumeHasRun) {
        resumeHasRun = true;
        return;
    }
    // Normal case behavior follows
}
Vamoose answered 30/5, 2011 at 11:43 Comment(3)
When I try to do this, I receive a "Suspended (exception RuntimeException)" when I try to set resumeHasRun = true. Any ideas?Modigliani
Instead of this you should use Activity's onRestart() method, it is intented exactly for that use case.Leroylerwick
Provided that your activity gets stopped (onStopped()), then @FenixVoltres is correct, in that, you should use onRestart().Marlite
L
15

The correct answer is to use Activity's onRestart() method. This is probably what you have been looking for.

Leroylerwick answered 12/3, 2012 at 12:5 Comment(3)
Not necessarily. onRestart() is called only if the activity has stopped.Supination
You're right, but the answer to above question was to move code from onResume() (which is called also when activity is created) to onRestart(), which is not, what was the point.Leroylerwick
@bowmanb Correct, but without knowing his intent, in most cases, onRestart() is what you'd want here.Marlite
D
1

You can't do anything, as this is how the Activity lifecycle works.

See http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle for a diagram that shows the lifecycle.

Disputatious answered 30/5, 2011 at 11:35 Comment(2)
ok thanks.. so if the onResume is called any time there is no way to make the activity to behave in a certain way at a first access, and in another way when is called back.. am I right?Lochner
@Lochner Yes, look at the onRestart() method. See @Fenix's answer.Marlite
M
1

As you can see in the API the Activity Lifecycle always calls onResume before showing the activity. http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

I guess you could make a global boolean for a first access and set it to false. Then override the onResume and check the variable. If false, set it to true and return, if true, call super.onResume.

Should work, but I don't know if it can be handled simpler and I don't have access to the sdk here to test it.

Murex answered 30/5, 2011 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.