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" />
本文介绍了一种方法,在Android应用的主活动中通过检查任务根、使用ActivityManager获取运行任务来优化应用启动性能,同时强调了需要在Manifest中添加权限以获取任务信息。
1750

被折叠的 条评论
为什么被折叠?



