So our app is handling when the app goes to background via this hack found elsewhere:
public static boolean isAppGoingToBackground(Context context)
{
boolean retVal = false;
ActivityManager mgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTasks = mgr.getRunningTasks(1);
if (!runningTasks.isEmpty())
{
// If the package name of the current activity doesn't equal the context's
// package name, then the user is no longer in the context's app.
ComponentName currentActivity = runningTasks.get(0).topActivity;
retVal = !currentActivity.getPackageName().equals(context.getPackageName());
}
return retVal;
}
However this doesn't handle if the screen is turned off via the power button, so we added a broadcast receiver for that event:
_receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction() == Intent.ACTION_SCREEN_ON)
{
//resume
}
else if(intent.getAction() == Intent.ACTION_SCREEN_OFF)
{
handleAppGoingToBackground(_context);
}
}
};
However neither of these give the correct response when users background the app using the task switcher:
If you background the app in this manner, the isGoingToBackground returns false. I was hoping there was another event in the broadcast receiver I could use to handle this event, but I haven't found anything. Is there anyway to know within your app when it is backgrounding in this manner?