Android 如何清除堆栈并关闭应用程序

Android 如何清除堆栈并关闭应用程序,android,android-activity,Android,Android Activity,我希望通过按下应用程序中的退出按钮来清除堆栈并关闭应用程序。这样,当我再次打开应用程序时,我需要从第一个活动开始启动应用程序 我正在使用下面的语句,但这些语句不起作用。我不想使用启动模式单个任务 intent.setFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP); intent.setFlags(intent.FLAG\u活动\u新任务) 按如下方式结束之前的所有活动: Intent intent = new Intent(this, MainActivi

我希望通过按下应用程序中的退出按钮来清除堆栈并关闭应用程序。这样,当我再次打开应用程序时,我需要从第一个活动开始启动应用程序

我正在使用下面的语句,但这些语句不起作用。我不想使用启动模式单个任务

intent.setFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
intent.setFlags(intent.FLAG\u活动\u新任务)

按如下方式结束之前的所有活动:

Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Exit me", true);
startActivity(intent);
finish();
然后在MainActivity
onCreate()
方法中添加此项以完成MainActivity

if( getIntent().getBooleanExtra("Exit me", false)){
    finish();
}

同时

关闭所有之前的活动,如下所示:

Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Exit me", true);
startActivity(intent);
finish();
然后在MainActivity
onCreate()
方法中添加此项以完成MainActivity

if( getIntent().getBooleanExtra("Exit me", false)){
    finish();
}

另外

@Crusader-我在我的应用程序中处于第10个屏幕。如果我按下这里的退出按钮,我不想导航到主要活动,但我想清除所有堆栈,我想导航到android home,我想在再次打开应用程序时从splash开始。谢谢你的上述建议。@Crusader-我在我的应用程序中处于第10个屏幕应用程序。如果我按下这里的退出按钮,我不想导航到主要活动,但我想清除所有堆栈,我想导航到android home,我想在再次打开应用程序时从splash开始。谢谢你的上述建议。