有没有一种方法可以准确检测到何时在Android中销毁了活动? [英] Is there a way to exactly detect when an activity is destroyed in android?

查看:56
本文介绍了有没有一种方法可以准确检测到何时在Android中销毁了活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java开发一个android应用程序,当活动完全销毁时,我需要触发一些数据库请求,如果用户按下后退"按钮或离开应用程序本身,则可能会发生...但是即使用户仍在活动中,我的应用程序中的onDestroy()函数也会被随机触发...我猜这可能是因为配置更改,但我无法为此找到合适的解决方案.

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

我正在使用的onDestroy()是这样的:

The onDestroy() that I am using is this:

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

任何帮助将不胜感激!

推荐答案

已解决:谢谢大家的回答...对我来说, onStop()完美地表现出来,无论是按后退按钮还是退出活动或应用程序本身,它在每种情况下都可以正常工作!

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

这篇关于有没有一种方法可以准确检测到何时在Android中销毁了活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆