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?

Guillermo Merino
  • 3,197
  • 2
  • 17
  • 34
Alessio
  • 183
  • 1
  • 1
  • 4

4 Answers4

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
}
Mark Roberts
  • 1,044
  • 6
  • 7
  • When I try to do this, I receive a "Suspended (exception RuntimeException)" when I try to set resumeHasRun = true. Any ideas? – proudgeekdad Jan 10 '12 at 04:25
  • 6
    Instead of this you should use Activity's onRestart() method, it is intented exactly for that use case. – Fenix Voltres Mar 12 '12 at 12:03
  • 1
    Provided that your activity gets stopped (`onStopped()`), then @FenixVoltres is correct, in that, you should use `onRestart()`. – Joshua Pinter Oct 14 '13 at 00:01
15

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

Fenix Voltres
  • 3,448
  • 1
  • 29
  • 36
  • Not necessarily. `onRestart()` is called only if the activity has stopped. – anon Sep 11 '12 at 01:57
  • 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. – Fenix Voltres Sep 11 '12 at 07:28
  • 1
    @bowmanb Correct, but without knowing his intent, in most cases, `onRestart()` is what you'd want here. – Joshua Pinter Oct 14 '13 at 00:02
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.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
  • 1
    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? – Alessio May 30 '11 at 12:44
  • @Alessio Yes, look at the `onRestart()` method. See @Fenix's answer. – Joshua Pinter Oct 14 '13 at 00:03
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.

MatF
  • 1,728
  • 2
  • 14
  • 31