79

好的,所以我有点难过如何处理这个问题。所以我有 MainActivity,可以从那里启动一个 Activity 到 DegreePlanActivity,然后从那里启动另一个 Activity 到 EditDegreePlan。我在 AndroidManifest 中将 EditDegreePlan 设置为 noHistory。问题是在他们保存 EditDegreePlan 后,它会向 DegreePlan 启动一个活动。因此,如果用户按 Back,他们必须按两次才能再次进入 MainActivity。我想摆脱它,所以他们只需要按一次。不过,我对如何做到这一点感到困惑。

如果我将 DegreePlanActivity 设置为 noHistory,那么他们在 EditDegreePlan 中无法按返回。

我已经尝试覆盖 onBackPressed 方法并启动 MainActivity 的意图。那么问题是他们必须多次按 Back 才能退出应用程序。

我应该怎么办?

4

11 回答 11

153

FLAG_ACTIVITY_CLEAR_TOP清除您的 Activity 堆栈,您可以使用以下代码:

Intent intent = new Intent(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

请记住,此标志仅清除 Intermediate Activities ,例如,如果您有 A,B,C 在您的Back Stack然后从 C Activity 到 D 使用此标志这不会清除Back Stack并且堆栈将为 A,B,C,D 但如果您使用此标志从 Activity D 转到 Activity A,B,C,D 活动将从堆栈中弹出,您将只有 A 在 Back Stack 中。

于 2013-01-01T16:53:17.690 回答
17

API >= 15 到 API 23 用户活动名称的简单解决方案。

 Intent nextScreen = new Intent(currentActivity.this, MainActivity.class);
 nextScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
 startActivity(nextScreen);
 ActivityCompat.finishAffinity(currentActivity.this);
于 2016-11-04T18:03:46.863 回答
15

我建议您在启动 EditDegreePlan-Activity 时使用startActivityForResult(),而不是简单地startActivity(),如Android 教程中所述。

然后在 EditDegreePlan-Activity 中调用

setResult(RESULT_OK);
finish();

如果您不期望来自 EditDegreePlan-Activity 的任何数据,那么您不必实施onActivityResult.

于 2014-05-05T15:03:45.383 回答
13

要从清单android:noHistory="true"内的后台堆栈中删除活动,请添加到清单文件中的活动。

请参阅下面的示例。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.activity"
      android:versionCode="1"
      android:versionName="1.0">
 <application android:name="MyApp" android:label="My Application">
    <activity android:name=".LoginActivity" 
      android:noHistory="true"> //add this line to your activity inside manifest
     <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
   </activity>
 </application>
</manifest>
于 2019-06-27T03:16:25.393 回答
7

看来,如果您根本不指定任何标志,您将获得所需的行为。后退按钮只会做正确的事情。要从您的代码中关闭活动,请使用finish()它与用户按下后退按钮具有相同效果的方法。因此,当您完成 EditDegreePlan 时,您将自动到达 DegreePlan,也无需调用任何Intents

于 2013-01-01T16:56:19.813 回答
6

您可以在开始新活动之前调用完成。这将从堆栈中删除当前活动,因此当您从下一个活动按后退按钮时,不会从堆栈历史记录中调用第一个活动。

Intent i = new Intent(MainActivity.this, NextActivity.class);
finish();  
startActivity(i);
于 2018-05-28T20:48:58.310 回答
5

这是您的流程:

MainActivity --> DegreePlanActivty --> EditDegreePlan--> DegreePlan --> MainActivty

在“DegreePlan”中覆盖这些方法

public void onBackPressed() {
   super.onBackPressed();
   Intent goToMainActivity = new Intent(getApplicationContext(), MainActivity.class);
   goToMainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Will clear out your activity history stack till now
   startActivity(goToMainActivity);
}
于 2013-01-01T17:03:47.787 回答
2

使用它来清除堆栈:

 menuIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
于 2017-06-22T05:14:54.547 回答
1
Intent intent = new Intent(getContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

startActivity(intent);
于 2019-07-17T15:39:59.490 回答
0

您可以按如下方式添加标志并启动 Activity,请尝试以下代码

Intent i = new Intent(activity, Payment.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(i);
于 2019-03-12T11:50:20.153 回答
0
This code should help you out: It is in Kotlin
private fun verifyIfUserIsLoggedIn(){
        val uid = FirebaseAuth.getInstance().uid
        if(uid== null){
            val intent = Intent(this, MainActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
            startActivity(intent)
        }
    }
于 2020-06-30T14:44:39.337 回答