how to know our app has gone to background in android

放肆的年华 提交于 2019-12-25 00:26:39

问题


When user goes to other apps from my app or presses device home button from my app or etc , then my app will be sent to background. And in the background am doing some stuff . So how to know is my app has gone to background . One solution will be to check if any of app activities are in onstart,onrestart,onresume,onpause,onstop if not then we can consider it in background. But this solution is tedious please help me if you have a easy solution


回答1:


Using onPause isn't tedious, and may be your only solution anyway. Create an abstract Activity class that all your Activity classes extend and do whatever you need to do in onPause there:

public abstract class BaseActivity extends Activity {

    @Override
    public void onPause() {

        super.onPause();
        // Do whatever you need to do in onPause here
    }
}

Using this example, all your Activity classes should now extend BaseActivity.



来源:https://stackoverflow.com/questions/14332437/how-to-know-our-app-has-gone-to-background-in-android

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