Android onCreate onResume

Alessio picture Alessio · May 30, 2011 · Viewed 70.3k times · Source

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?

Answer

Mark Roberts picture Mark Roberts · May 30, 2011

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
}