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

主宰稳场 提交于 2019-12-07 23:23:37

问题


I need to reset notification when user removes my app from background.So I need to have an event of remove app from background.

Please help me out. Suggestion appreciated. Thanks. Kind Regards.


回答1:


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()




回答2:


   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);
    }



回答3:


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



来源:https://stackoverflow.com/questions/27700631/how-to-detect-event-when-user-removes-app-from-background-in-android

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