Code example for ActivityManager

Methods: getRunningTasks

0
     */ 
    static void handleApplicationClosing(final Context context, int keyCode) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            ActivityManager am = (ActivityManager) context
                    .getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningTaskInfo> tasks = am.getRunningTasks(2);
 
            RunningTaskInfo currentTask = tasks.get(0);
            RunningTaskInfo nextTask = tasks.get(1);
 
            // if we're looking at this application's base/launcher Activity, 
            // and the next task is the Android home screen, then we know we're 
            // about to close the app 
            if (currentTask.topActivity.equals(currentTask.baseActivity)
                    && nextTask.baseActivity.getPackageName().startsWith("com.android.launcher")) {
                DroidFuApplication application = (DroidFuApplication) context
                        .getApplicationContext();
                application.onClose();
            } 
        } 
Stop searching for code, let great code find you!  Add Codota to your java IDE