Search code examples
javaandroidandroid-studioandroid-activityondestroy

Is there a way to exactly detect when an activity is destroyed in android?


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!!


Solution

  • 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!!