简体   繁体   English

在Android应用程序中管理活动树

[英]Managing a tree of activities in android application

I think I didn't clearly asked this question before (because I didn't get the answer that I needed) and it was a mistake to post this version here , so I'll try one last time. 我觉得我没问清楚这个问题之前 (因为我没有得到我所需要的答案),它是发布这个版本的错误在这里 ,所以我会尽力最后一面。

I have a big tree of activities where there is one root-activity with 6 list items, each item leads to its own activity with its own list of items, etc. One generation of the tree can be displayed in one activity. 我有一棵大的活动树,其中有一个包含6个列表项的根活动,每个项目都导致具有自己的项列表的自己的活动,等等。在一个活动中可以显示一代树。 So there is only 1 activity for the root and 1 for all of its children(just different list items which are displayed during the runtime depending on the previously chosen item). 因此,根只有1个活动,其所有子元素只有1个活动(只是运行时期间显示的列表项不同,具体取决于先前选择的项)。 Navigation obviously should work in both directions - forward(get closer to the leaves of the tree) and backwards(get closer to the root). 导航显然应该在两个方向上起作用-向前(靠近树的叶子)和向后(靠近树的根)。 Hops between generations are also possible(eg we can jump from the 1 generation straight to the 3 and backwards). 两代之间的跳跃也是可能的(例如,我们可以从第一代直接跳到第三代,然后向后跳)。 I think that creating intents every time user goes to another activity is not reasonable. 我认为每次用户进行其他活动时创建意图都是不合理的。 Is there any pattern or good practice to manage multiple activities like in this case? 在这种情况下,是否有任何模式或良好实践来管理多个活动? Maybe in each activity should be stored a static class which returns its intent or another management class created? 也许在每个活动中都应存储一个返回其意图的静态类或创建的另一个管理类?

In Android activities are managed on a (back-) stack. 在Android中,活动是通过(后退)堆栈进行管理的。 Which means you can start a new activity and it is pushed onto the stack and is the activity shown. 这意味着您可以开始一个新的活动并将其推入堆栈,并显示该活动。 When going back via finish() this top activity is popped from the stack. 通过finish()返回时,此顶级活动将从堆栈中弹出。

Stacks and trees work together quite well. 栈和树可以很好地协同工作。 When you move around in a tree you can keep the path to the root on a stack. 当您在树中四处移动时,可以将到根的路径保留在堆栈中。 This way managing trees of activities is what Android is actually made for. 这种管理活动树的方法实际上就是Android的目的。

Going down in the tree means calling the activity of the child node. 在树中下降意味着调用子节点的活动。 Going up again is done by simply finishing your activity. 只需完成您的活动即可再次上升。 You automatically appear at the activty one level up in the tree. 您会自动出现在树中的活动一级。

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

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