How to detect event when user removes app from background in android?

大兔子大兔子 提交于 2019-12-06 12:30:57

Official android documentation

Activity.onDestroy()

The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

http://developer.android.com/reference/android/app/Activity.html#onDestroy()

   Whenever an app is removed from background it's processID is changed. 

Simply compare those pid(Process ID).

    int oldProcessID = SettingsSharePref.readProcessID();
    int newProcessID = android.os.Process.myPid();//getProcessID(mContext);/

    if( oldProcessID != newProcessID ){
        //if it killed or started as a new process...
        SettingsSharePref.saveProcessID(newProcessID);
    }

In the life cycle of android . you can find onDestroy() method, with the help of this you can have a broadcast receiver or service based on your requirement. Hope you got a solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!