In your main activity add this code on the top of the onCreate
method:
ONE:
if (!isTaskRoot()) { final Intent intent = getIntent(); final String intentAction = intent.getAction(); if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) { finish(); } }
or
TWO:
ActivityManager manager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE ); List<RunningTaskInfo> tasks = manager.getRunningTasks(Integer.MAX_VALUE); for (RunningTaskInfo taskInfo : tasks) { if(taskInfo.baseActivity.getClassName().equals(<your package name>.<your class name>) && (taskInfo.numActivities > 1)){ finish(); } }
don't forget to add this permission in your manifest.
< uses-permission android:name="android.permission.GET_TASKS" />