質問

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.

Is anybody aware of a way to make it like the "don't keep activities" behaviour,e.g. 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. 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. Then you can use `Intent.FLAG_ACTIVITY_REORDER_TO_FRONT' to transition from one activity to the next.

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. 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).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top