0

By using onPause(), I can check if an activity is going to background or not. My problem is this onPause() is call each time even if I am going to another activity from this activity.

So my main problem is ,if I am minimizing the app by pressing home button I want to remove app from task manager and this code is write in each activity when app is going to background BUT not using onPause().

My code snippet is below ->

@Override
public void onPause() {
    if (isBackgroundRunning(this)){
        ExitActivity.exitApplication(getApplicationContext());
    }
    super.onPause();
}

public static boolean isBackgroundRunning(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
        if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            for (String activeProcess : processInfo.pkgList) {
                if (activeProcess.equals(context.getPackageName())) {
                    //If your app is the process in foreground, then it's not in running in background
                    return true;
                }
            }
        }
    }

    return true;
}

ExitActivity is used for remove the app from task manager.

public class ExitActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    finish();
}

public static void exitApplication(Context context) {
    Intent intent = new Intent(context, ExitActivity.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

    context.startActivity(intent);
}

}

I saw most of the questions related to this but can't find out the solution. Please help me. Thanks in advance :)

Ekta Bhawsar
  • 746
  • 11
  • 26
  • you cant find a solution because there is no solution, you cant listen for home button events for security reasons. why do you need to kill your app anyway? – tyczj Jul 12 '17 at 12:52
  • @tyczj actually I'm working on Health app which has confidential data so we used 2 hour session management from backend services, When app is minimized and after 3 hours or later come to foreground then open same point where app is minimized and user will fill all the details and then submit after that webservice calling he got session is invalid. – Ekta Bhawsar Jul 12 '17 at 13:13
  • 1
    Then just create an expiration time stamp in shared preferences and update it to current time + 2 hours in every onResume then check time in onCreate. if its past the time start the app over – tyczj Jul 12 '17 at 13:18

1 Answers1

-1

Try calling finish() in your onPause() method in your apps parent/launcher activity. That should kill the app when the user presses the home button

Joshua
  • 589
  • 1
  • 5
  • 19