返回活动而不重新创建(不调用onCreate()) [英] Go back to Activity without recreating (without invoking onCreate())

查看:64
本文介绍了返回活动而不重新创建(不调用onCreate())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问类似的问题:

返回上一个屏幕而不创建新实例

但是我不希望Activity A进行onCreate回调,我想以相同状态返回到先前的实例.如何在不调用多个finish()的情况下实现这一目标?

However I don't want Activity A go through onCreate callback, I would like to return to previous instance in the same state. How to achieve that without calling multiple finish() ?

推荐答案

在应用程序中导航时,无需完成()活动.取而代之的是,您可以将活动"保持在后台,而仍然可以实现您的目标.假设您有4种这样的活动:

There's no need to finish() activities as you navigate through your application. Instead, you can maintain your Activity back-stack and still achieve your goal. Let's say you have 4 activites like so:

A-> B-> C-> D

A --> B --> C -->D

其中D是最高活动,A是根活动.如果您要退回"活动B,则需要做两件事,以避免碰到B的onCreate方法.

where D is the topmost activity and A is root activity. If you are 'falling back' to activity B, then you'll need to do two things in order to avoid hitting B's onCreate method.

1.)使B成为" SingleTask "活动.您可以在Android清单中执行此操作.简而言之,这意味着此任务中将仅存在B的一个实例".如果B在被调用时已经在运行,那么它将被简单地带到前面.这就是您的做法.

1.) Make B a "SingleTask" activity. You can do this in the Android Manifest. To be brief, this means that only one 'instance' of B will ever exist within this Task. If B is already running when it's called then it will simply be brought to the front. This is how you do that.

  <activity
        android:name=".ui.MyActivity"
        android:launchMode="singleTask"/>

但是您不想只将B放在前面.您想退回"到B,以便您的堆栈看起来像

But you don't want to just bring B to the front. You want to 'fall back' to B so that your stack looks like

A-> B

2.)将此标志添加到开始" B的意图中.这样可以确保删除C和D.

2.) Add this flag to your intent that 'starts' B. This ensures that C and D are removed.

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

现在,当"D"向B调用新的Intent时,B将恢复,而C和D将被删除. B将不会重新创建,它只会调用onNewIntent.

Now when 'D' calls new Intent to B, B will be resumed and C and D will be removed. B will not be recreated, it will only call onNewIntent.

这篇关于返回活动而不重新创建(不调用onCreate())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆