如何处理导航相关活动之间 [英] How to Handle Navigation between Dependent Activities

查看:118
本文介绍了如何处理导航相关活动之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何根据它的同时保持其母公司提供的背景儿童更新活动。

请参阅下面的示例层次结构。有一个液流上下。使用该应用程序会去是这样的:

See the example hierarchy below. There is a flow up and down. Using the app would go something like this:

在家庭活动选择团队1 - >活动一个膨胀的基础上选择
。 活动一上选择播放器2 - > b活动膨胀的基础上选择
。 选择要添加统计b活动上 - >活动ç膨胀,并允许添加一个统计。

Select Team 1 on the Home activity --> Activity A inflates based on the selection.
Select Player 2 on Activity A --> Activity B inflates based on the selection.
Select to Add a Stat on Activity B --> Activity C inflates and allows a stat to be added.

用户返回到活动B,基于C.
必须保持播放器2背景和更新 用户返回到活动A,它必须保持球队1背景和依据B.
更新 用户返回到主页,必须更新基于A.

User returns to Activity B, which must maintain the Player 2 context and update based on C.
User returns to Activity A, which must maintain the Team 1 context and updated based on B.
User returns to Home, which must update based on A.

这两个活动A和B充气基于父母的选择,但必须更新,如果一个变化是在一个子活动。

Both Activities A and B inflate based on parent selections, but must update if a change is made in a child activity.

例如,让我们专注于活动B.它知道膨胀的玩家1,因为这个消息是从一个活动传递一个意图

For example, let's focus on Activity B. It knows to inflate for Player 1 because this info was passed from Activity A in an intent.

  1. 如果我是去到活动C和preSS 返回,B不会更新。
  2. 如果我是去C和preSS 向上,B会崩溃,因为它会尝试进行新的实例,也不会知道什么球员膨胀(在定义的onCreate())。
  1. If I were to go to Activity C and press Back, B wouldn't update.
  2. If I were to go to C and press Up, B would crash because it would try to make a new instance and wouldn't know what player to inflate (defined in onCreate()).

我想声明B带一个singleTop launchMode ,但它只是重新启动并基于C的信息不更新。根据我有限的经验/研究,我看到下面的选项。

I tried declaring B with a "singleTop" launchMode, but then it just restarts and doesn't update based on C's info. Based on my limited experience/research, I see the following options.

  1. 在存储中的活动的吹气变量为静态的(所以他们不会被丢失创建一个新的实例时)
  2. 启动我认为通胀率(通过片段)在活动的 ONSTART()方法,因此,这将重新启动时刷新。 (这会帮助吗?怎么样,我所有的通胀发生在片段?我害怕生命周期神内的事实。)
  1. Store an Activity's inflater variables as static (so they won't be "lost" when creating a new instance)
  2. Initiate my view inflation (via Fragment) in the Activity's onStart() method so that it would refresh when Restarting. (Would this help? What about the fact that all of my inflation happens inside of a fragment? I fear the Lifecycle gods.)

我是新来了很多这一点,将AP preciate任何帮助。谢谢!

I'm new to a lot of this and would appreciate any help. Thanks!

编辑:我试着做运动员的活动B中的静态变量,让活动一个设置。这似乎是工作。我可以删除singleTop发射模式,然后导航的的从C加载B的新实例的信息刷新。

I've tried making "player" a static variable in Activity B and letting Activity A set it. This seems to be working. I can remove the "singleTop" launch mode and then navigating up from C loads a new instance of B with the info refreshed.

不过,这的感觉的像一个解决办法,我想有人对这种做法发表评论,也许提供了另一种选择。谢谢你。

However, this feels like a workaround and I would like someone to comment on this approach and maybe offer an alternative. Thanks.

推荐答案

你有没有考虑过用片段和您的家庭活动处理所有的事务,并backstack带参数,并呼吁将其分解内部方法来更新UI?

Have you considered using fragments and your home activity handling all the transaction and backstack with arguments and calls to fragment internal methods to update UI?

如果你真的很喜欢使用的活动,那么你应该使用启动活动的结果。您可以将结果调用活性和相应更新。

If you really like to use activities, then you should use start activity for result. You can pass result to calling-activity and update accordingly.

从活动中得到一个结果

这篇关于如何处理导航相关活动之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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