0

I am making an android app in java in which I need to trigger some database requests whenever an activity is completely destroyed which would probably happen if the user presses the back button or leaves the app itself... But the onDestroy() function in my app is randomly getting triggered even when the user is still on the activity... I guess the probable reason for this is configuration changes but I am not able to figure out a proper solution for this.

Is there a way we could exactly detect when an activity is left by a user avoiding any in-page configuration changes??

The onDestroy() that I am using is this:

@Override
protected void onDestroy() {
   /// do smthng
   super.onDestroy();
}

Any help would be appreciated!!

Aarav Garg
  • 11
  • 1
  • 3

3 Answers3

1

Solved: Thank you for the answer guys... For me onStop() worked out perfectly and it is working in every case whether it might be pressing the back button or exiting the activity or the app itself!!

Aarav Garg
  • 11
  • 1
  • 3
0

If you want to check if the user ended the activity, meaning pressed back, do this:

@override
public void onBackPressed(){

//do something before we finish the activity

super.onBackPressed();

}

If you want to check when user, goes to next activity, then resturns to the same activity:

@override
public void onResume(){

//do something when return back to this activity

super.onResume();

}


@override
public void onPause(){

//do something before going to another activity

super.onPause();

}

onDestroy is called when the activity is destroyed or finished and not guaranteed to be called always, don't depend on it

Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22
  • Yeah....I already tried using `onBackPressed()` but that works only for back press obviously but I need something other than that also because I want a small end routine whenever the activity is left..... Now leaving the activity also involves leaving the app!! – Aarav Garg Apr 18 '20 at 08:25
  • Maybe you should change the question, how to detect when user leaves activity and app at same time. – Hasan Bou Taam Apr 18 '20 at 08:32
  • Well in my case the user could only leave the activity if he exits the app or presses the back button coz that's an end activity and there's no further intent from this activity!! – Aarav Garg Apr 18 '20 at 08:53
  • if your app is single activity then what is the problem? – Hasan Bou Taam Apr 18 '20 at 08:56
  • No, it's not single activity... From the activity that I have a problem with there is no intent. You can just go back or close the app... – Aarav Garg Apr 18 '20 at 09:03
  • There is no any guarantee call back when kill the app or uninstall. – Thirumalai Apr 18 '20 at 09:13
0

We can check on whether our application is foreground or background based on the activity entering and exiting the foreground by implementing ActivityLifecycleCallbacks.

Good reference : https://medium.com/@iamsadesh/android-how-to-detect-when-app-goes-background-foreground-fd5a4d331f8a

Quoting from the above article,

@Override
public void onActivityStarted(Activity activity) {
    if (++activityReferences == 1 && !isActivityChangingConfigurations) {
        // App enters foreground
    }
}

and,

@Override
public void onActivityStopped(Activity activity) {

    isActivityChangingConfigurations = activity.isChangingConfigurations();
    if (--activityReferences == 0 && !isActivityChangingConfigurations) {
        // App enters background
    }
}

by which we can make sure that our app is in foreground or not. Here you always have the control of what activity is in foreground based on which you can check and execute the logic.

Tom Taylor
  • 3,344
  • 2
  • 38
  • 63