Finish parent and current activity in Android

前端 未结 16 3118
误落风尘
误落风尘 2020-11-27 03:40

I have 3 activities. Activity A which leads to activity B, which in turn can go back to activity A or start activity C. However, if I press back in activity C the app should

16条回答
  •  盖世英雄少女心
    2020-11-27 04:09

    The following code should do it for you. Almost in all other approaches your app will remain in recent apps. Javadoc can be found at, https://developer.android.com/reference/android/app/ActivityManager.AppTask.html#finishAndRemoveTask()

                ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
                if(am != null) {
                    List tasks = am.getAppTasks();
                    if (tasks != null) {
                        tasks.get(0).finishAndRemoveTask();
                    }
                }
    

提交回复
热议问题