繁体   English   中英

Android:清除后退堆栈

[英]Android: Clear the back stack

在 Android 中,我有一些活动,比如说 A、B、C。

在 A 中,我使用此代码打开 B:

Intent intent = new Intent(this, B.class);
startActivity(intent);

在 B 中,我使用此代码打开 C:

Intent intent = new Intent(this, C.class);
startActivity(intent);

当用户点击 C 中的按钮时,我想返回 A 并清除返回堆栈(关闭 B 和 C)。 因此,当用户使用后退按钮 B 和 C 不会出现时,我一直在尝试以下操作:

Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent);

但是,如果我在返回活动 A 时使用后退按钮,B 和 C 仍会出现。我该如何避免这种情况?

尝试按照FLAG_ACTIVITY_NEW_TASK文档中的描述添加FLAG_ACTIVITY_CLEAR_TOP

这种启动模式也可以和FLAG_ACTIVITY_NEW_TASK配合使用,效果很好:如果用于启动一个任务的根活动,它将把该任务当前正在运行的任何实例带到前台,然后将其清除到其根状态。 例如,当从通知管理器启动活动时,这尤其有用。

因此,您启动A的代码将是:

Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent);
CurrentActivity.this.finish(); // if the activity running has it's own context


// view.getContext().finish() for fragments etc.
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

添加相关活动的清单文件怎么样:

android:noHistory="true"

到 B 和 C 的活动定义? 它们不会被添加到 backstack 中。 不确定这是否是您想要的。

这困扰了我很长时间。最后我通过这样做解决了这个问题:

在片段中,使用:

Intent intent = new Intent(view.getContext(), A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent);

在 Activity 中,使用(与片段相比,再添加一个意图标志Intent.FLAG_ACTIVITY_CLEAR_TASK ):

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

从 API 16 (Jelly Bean) 开始,您只需调用finishAffinity()

现在,您还可以使用兼容性库调用ActivityCompat.finishAffinity(Activity activity)

确保将清单中的 taskAffinity 设置为该组活动唯一的包名称。

查看更多信息:
http://developer.android.com/reference/android/support/v4/app/ActivityCompat.html#finishAffinity%28android.app.Activity%29

  1. android:launchMode="singleTop"添加到活动 A 清单中的活动元素
  2. 然后在启动Activity A时使用intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

这意味着当 Activity A 启动时,它上面的所有任务都会被清除,因此 A 是最上面的。 以 A 为根创建一个新的返回堆栈,并使用singleTop确保您只启动 A 一次(因为 A 现在由于..._CLEAR_TOP而位于顶部)。

尝试使用

intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

并不是

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

如果您的应用程序具有最低 sdk 版本 16,那么您可以使用finishAffinity()

完成此活动以及当前任务中紧跟其下的所有具有相同关联的活动。

这对我有用 在 Top Payment 屏幕中删除所有 back-stack 活动,

 @Override
public void onBackPressed() {
         finishAffinity();
        startActivity(new Intent(PaymentDoneActivity.this,Home.class));
    } 

http://developer.android.com/reference/android/app/Activity.html#finishAffinity%28%29

根据Wakka从历史堆栈删除活动...


android:noHistory="true"属性添加到AndroidManifest.xml中的<activity>中,如下所示:

    <activity android:name=".MyActivity"
        android:noHistory="true">
    </activity>

使用finishAffinity(); 在任务 C 中启动任务 A 以清除 backstack 活动。

为了将来的研究,试试这个代码。

Intent intent = new Intent(context, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

使用此代码启动新活动并关闭或销毁所有其他活动堆栈或返回堆栈。

Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Use finishAffinity() to clear all backstack with existing one.

Suppose, Activities A, B and C are in stack, and finishAffinity(); is called in Activity C, 

    - Activity B will be finished / removing from stack.
    - Activity A will be finished / removing from stack.
    - Activity C will finished / removing from stack.

我找到了一个有趣的解决方案,可能会有所帮助。 我在我的onBackPressed()方法中做到了这一点。

finishAffinity();
finish();

FinishAffinity删除现有活动与其堆栈的连接。 然后完成帮助您退出该活动。 这将最终退出应用程序。

在 kotlin 中,它与 java 几乎相同。 只有| 符号由文本替换。 所以,它是这样写的——

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)

我发现这里的答案有点误导,因为原始问题中的代码对我来说似乎工作正常?

A 是根活动,从 B 或 C 启动它,仅使用 FLAG_ACTIVITY_CLEAR_TOP 确实会从后堆栈中删除 B 和 C。

我尝试了所有解决方案,但没有一个单独为我工作。 我的解决方案是:

声明Activity ASingleTop通过使用[android:launchMode="singleTop"]在Android清单。

现在在从任何地方启动A添加以下标志。 它将清除堆栈。

Intent in = new Intent(mContext, A.class);
in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
startActivity(in);
finish();

Intent.FLAG_ACTIVITY_CLEAR_TOP在这种情况下将不起作用。 请使用(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)

有关更多详细信息,请查看文档。

科特林示例:

      val intent = Intent(this@LoginActivity, MainActivity::class.java)
      intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
      startActivity(intent)
      finish()

在我看来,您需要使用startActivityForResult()Activity B启动Activity C 当您单击Activity C的按钮时,调用setResult(RESULT_OK)finish()以便结束Activity C Activity B ,您可以让onActivityResult()也通过调用其自身的finish()来响应,然后您将被带回Activity A

先进的、可重用的 Kotlin:

您可以使用 setter 方法直接设置标志。 在 Kotlin 中or是 Java 按位或的替代品| .

intent.flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK

如果多次使用它,请创建一个 Intent 扩展函数

fun Intent.clearStack() {
    flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}

然后你可以在开始意图之前直接调用这个函数

intent.clearStack()

如果您需要在其他情况下添加额外标志的选项,请向扩展函数添加一个可选参数。

fun Intent.clearStack(additionalFlags: Int = 0) {
    flags = additionalFlags or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}

logout.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); logout.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

您可以使用此示例从Activity C调用您的Activity A

Intent loout = new Intent(context, LoginActivity.class); loout.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); context.startActivity(loout);

 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

为我添加Intent.FLAG_ACTIVITY_CLEAR_TASK解决了问题

Intent i = new Intent(SettingsActivity.this, StartPage.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP  | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
finish();

给定的代码工作正常。 我在应用程序生命周期示例中尝试过。

在使用标志 FLAG_ACTIVITY_CLEAR_TOP 启动活动 A 后,我没有在后堆栈中获得 B 和 C

我在开始新意图后调用了activity_name.this.finish()并且它对我有用。

I tried "FLAG_ACTIVITY_CLEAR_TOP" and "FLAG_ACTIVITY_NEW_TASK"

但它对我不起作用......我不是建议使用这个解决方案,但如果设置标志对你不起作用,那么你可以尝试这个......但我仍然建议不要使用它

在意图中添加 NO History Flag。

在活动B中,开始活动C,如下所示>>>>>>

Intent intent = new Intent(this, C.class);
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY); 
startActivity(intent);
finish();

将其添加到您的Activity BActivity C

android:noHistory="true"

Override onBackPressed功能,避免背部采用了压return

@Override
public void onBackPressed() {
   return;
}

在清单中

android:launchMode="singleTask"

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

使用setFlags()方法清除背面打开所有活动关闭并启动yourActvity

Intent intent = new Intent(getApplicationContext(), yourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

这对我来说对onBackPressed很有用:

public void onBackPressed()
{
    Intent intent = new Intent(ImageUploadActivity.this, InputDataActivity.class);

    Intent myIntent = new Intent(this, ImageUploadActivity.class);
    myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
   finish();
}

除了FLAG_ACTIVITY_CLEAR_TOP ,您可以尝试添加Intent.FLAG_ACTIVITY_SINGLE_TOP还有:

intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM