如何检测整个应用程序是否在android中关闭 [英] How to detect if whole application is close in android

查看:99
本文介绍了如何检测整个应用程序是否在android中关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,通过按主屏幕按钮关闭应用程序,并且onDestroy()不会调用.我想在整个应用程序关闭时调用一个方法,而我不会在所有活动的onDestroy()中调用我的方法.

Some times application be closed by pressing home button and onDestroy() doesn't call. I want to call a method when whole application is closed and I'm not going to call my method in all activities's onDestroy().

推荐答案

在appication类中实现LifecycleObserver然后用作打击:

implements LifecycleObserver inside appication class an then use as blow:

public class App extends Application implements LifecycleObserver{

    @SuppressLint("CheckResult")
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onMoveToForeground() {


    }

    @SuppressLint("CheckResult")
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onMoveToBackground() {

    }
}

还可以使用其他事件,例如Lifecycle.Event.ON_DESTROY或ON_CREATE

Also can use other events like Lifecycle.Event.ON_DESTROY or ON_CREATE

这篇关于如何检测整个应用程序是否在android中关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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