问题
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