Usman Rana
Usman Rana

Reputation: 2142

Unable to detect when app is reentered from background other than onStop method?

I want to refresh the app screen whenever user minimizes the app and reopens it from background. The issue is that if i apply my logic in onStop method then it will be called in that case too when i open another activity on top of existing page when app is open. In iOS there is a method that informs them when app is re-entered from background but how can i achieve this in android?

Upvotes: 0

Views: 29

Answers (1)

Itamar Kerbel
Itamar Kerbel

Reputation: 2568

You should use OnResume().

There is the famous graph taken from here that might help you. enter image description here

If you would like to know if the OnResume() is called after onStart or onPause(), have a boolean flag in your activity like:

void onPause () {
    bPaused = true;
}

void onResume() {

    //check the bPaused member
    if (bPaused == true){
        bPaused = false; // reset the flag
        //do what ever you want to do as the app was resumed
    } else {
        // we are entring this code after onStart and onPaused was not called yet.
    }

}

Upvotes: 2

Related Questions