Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android onCreate onResume

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?

like image 994
Alessio Avatar asked May 30 '11 11:05

Alessio


People also ask

What is onResume in Android?

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.

Is onResume called after onCreate?

onResume() will never be called before onCreate() . Show activity on this post. onResume() will always be called when the activity goes into foreground, but it will never be executed before onCreate() .

What is the difference between onPause and onResume?

After the onResume() method has run, the activity has the focus and the user can interact with it. The onPause() method runs when the activity stops being in the foreground. After the onPause() method has run, the activity is still visible but doesn't have the focus.


2 Answers

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
}
like image 163
Mark Roberts Avatar answered Sep 28 '22 11:09

Mark Roberts


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

like image 23
Fenix Voltres Avatar answered Sep 28 '22 13:09

Fenix Voltres