简体   繁体   English

实施类似于“不参加活动”的行为

[英]implement behaviour similar to “don't keep activities”

There's a scenario in my app which it could go activity a->activity b->activity a->activity b...infinitely, and eventually it'll get OOM. 我的应用程序中有一个场景,它可以无限执行活动a->活动b->活动a->活动b ...,最终得到OOM。

Is anybody aware of a way to make it like the "don't keep activities" behaviour,eg the activities will be killed to release memory but still in history, so it can be recreated as user navigates back? 是否有人意识到像“不保留活动”这样的行为的方式,例如活动将被杀死以释放内存但仍保留在历史记录中,因此可以在用户向后导航时重新创建它?

This isn't possible. 这是不可能的。 The activities need to be present in the stack for Android to be able to go back to them. 这些活动必须存在于堆栈中,Android才能返回到它们。 What you can do is to keep track of the data that the activities are managing yourself, so that as the user goes from ActivityA to ActivityB to ActivityA you keep pushing a data packet onto a stack that is available to both activities. 您可以做的是跟踪活动正在管理的数据,以便当用户从ActivityA转到ActivityB再到ActivityA时,您可以将数据包不断推入两个活动都可以使用的堆栈中。 Then you can use `Intent.FLAG_ACTIVITY_REORDER_TO_FRONT' to transition from one activity to the next. 然后,您可以使用Intent.FLAG_ACTIVITY_REORDER_TO_FRONT从一个活动过渡到下一个活动。

In this case you will only ever have one instance of ActivityA and one instance of ActivityB, but they should be able to present the user a different view each time they get control by just looking at the data packet on the top of the stack. 在这种情况下,您将永远只有一个ActivityA实例和一个ActivityB实例,但是每次他们获得控制权时,只要查看堆栈顶部的数据包,他们就应该能够向用户呈现不同的视图。 When the user presses the back button , you should pop the top data packet off the stack and then start the appropriate activity for it (using `Intent.FLAG_ACTIVITY_REORDER_TO_FRONT' to ensure that you don't create a new instance). 当用户按下后退按钮时 ,您应该从堆栈中弹出顶部的数据包,然后为其进行适当的活动(使用“ Intent.FLAG_ACTIVITY_REORDER_TO_FRONT”来确保您不创建新实例)。

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

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