android删除活动,如何从堆栈中删除最后一个活动?(How remove last activity from the stack?)...

如何从堆栈中删除最后一个活动?(How remove last activity from the stack?)

我有3个活动(A,B,C),流程是:

A -> B -> C

当我点击回来时,流程是:

C -> B -> A

但我想要这个:

C -> A

C -> B -> A

怎么样?

I have 3 activitys (A, B, C), the flow is:

A -> B -> C

when i click to back, the flow is:

C -> B -> A

but i want this:

C -> A

not

C -> B -> A

how?

原文:https://stackoverflow.com/questions/31796246

更新时间:2020-02-02 20:50

最满意答案

有几种方法可以做到这一点。

如果您知道哪个活动不想从堆栈中删除,那么您所要做的就是在调用

startActivity(intent)之后调用

finish()

startActivity(intent)到下一个活动,因此此活动将从流中排除,请参阅示例如下:

Intent intent = new Intent(...);

startActivity(intent);

finish();

如果决定是基于与用户的交互而发生的,则可以调用startActivityForResult()来启动下一个活动,当活动结束时,您必须返回RESULT值。

There are a few ways to do this.

If you know which activity you don't want to remove from the stack, all you have to do is call

finish() after your call

startActivity(intent) to the next activity, so this activity will be excluded from the flow, see the example below:

Intent intent = new Intent(...);

startActivity(intent);

finish();

If the decision will happen based on an interaction with the user, you can call startActivityForResult() to start the next activity, and when the activity ends, you have to return a RESULT value.

Here's a good documentation about how to use startActivityForResult(): http://developer.android.com/training/basics/intents/result.html

相关问答

显然,我想针对这种可能的内存泄漏做点什么。 什么可能的内存泄漏? Android将根据需要销毁活动以回收内存。 如果用户按下按钮进入一个被销毁的Android,则活动将使用其原始Intent重新创建,并通过onSaveInstanceState()传递您填写的Bundle。 如果堆栈的大小大于某个数字,是否可以从活动堆栈的底部删除该条目? 那么堆栈只会记住最新的X(例如30)活动? 不是我知道的。 再说一次,你不应该需要它。 Obviously, I'd like to do something

...

启动B时,不要在A中调用startActivity ,而是调用startActivityForResult 。 然后,你的A活动,处理onActivityResult 。 现在,在B中,当你打开C时,在调用完成之前调用setResult 。 这将允许您设置一些数据以传递回A的onActivityResult方法。 传递一个标志,表示A应该关闭,然后调用finish 。 在A的onActivityResult处理该标志。 这样,每个活动都有责任自行关闭,而且你不会人为地弄乱后台。 在简单的A,B,C

...

FLAG_ACTIVITY_CLEAR_TOP清除您的活动堆栈,您可以使用代码: Intent intent = new Intent(this, Activity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);

请记住,此标志清除中级活动,例如,如果您的Back Stack有A,B,C,则使用此标志从C Activity转到D,这不会清除Back Stack ,Stack将是A,B,

...

这是因为FB Activity和您的Activity都属于同一个任务。 请参阅有关如何在单独任务中启动活动的官方文档 。 具体来说,Intent Flag FLAG_ACTIVITY_NEW_TASK用于在单独的任务中启动Activity。 This is so because both FB Activity and your Activity belong to same task. Refer to Official Documentation about how to launch an

...

以下行将完成您正在进行的活动,并将开始相同的活动: finish();

startActivity(getIntent());

The below lines will finish the activity in which you are working and will start the same activity: finish();

startActivity(getIntent());

您需要做的就是在onActivityResult()调用finish() onActivityResult() 。 一旦共享完成,这将破坏该活动。 例如: @Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

callbackManager.onActivityResult(requestCode, resultCode, data);

finish();

...

有几种方法可以做到这一点。 如果您知道哪个活动不想从堆栈中删除,那么您所要做的就是在调用 startActivity(intent)之后调用 finish() startActivity(intent)到下一个活动,因此此活动将从流中排除,请参阅示例如下: Intent intent = new Intent(...);

startActivity(intent);

finish();

如果决定是基于与用户的交互而发生的,则可以调用startActivityForResult()来启动下一个活动

...

检查这是你需要的吗?

android:noHistory="true">

What I ended up doing was that when Activity_A calls up Activity_B, it doesn't create a new Activity but instead just replaces it. So then when I get rid of B (

...

你说的FLAG_ACTIVITY_REORDER_TO_FRONT看起来很有希望。 它再次启动相同的活动,但在历史堆栈中移动它。 首先,这将恢复原始状态(图片B1),但您确实在活动B中收到新的Intent。使用此更新数据: @Override

public void onNewIntent(Intent newIntent) {

super.onNewIntent(newIntent)

// update picture here from new newIntent

}

还有其

...

实际上我正在阅读用于启动活动的不同标志,并找到了FLAG_ACTIVITY_REORDER_TO_FRONT标志。 这完全符合我的要求,它恢复已经在堆栈上的活动而不创建另一个实例,并且不会发出或更改任何现有的requestCodes。 actually I was reading about the different flags used to start activities and found the FLAG_ACTIVITY_REORDER_TO_FRONT flag. This acc

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值