2

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322

1 Answers1

0

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

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • That's wrong. It isn't impossible. You can start every activity with the `Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK` flags and finish the underlying activity and override on back pressed to start the activity again. Though, it isn't a good practice, but not impossible. – Leandros Jan 24 '13 at 20:08
  • @Leandros what I meant by "this isn't possible" is the wish from OP that the user can go back to previous activities without them being in the activity stack (ie: taking up memory). Of course you can do this, but the problem for OP will be managing the state of those activities that are no longer in the back stack. He will have to do this himself, because Android cannot do this if the activities are not in the back stack. So I suggested a way for OP to manage this himself. I didn't mean to imply that it wasn't possible to build an app with this behaviour, just that Android won't do it for you. – David Wasser Jan 24 '13 at 20:19
  • Oh yeah, sure. You have to manage the activity state on your own. – Leandros Jan 24 '13 at 20:24
  • Thanks guys for taking time to answering my questions. I thought about both options too. Yeah if android doesnt have a built in mechanism then I'll probably just go for the first option(actually, in that case it might be easier to just use two fragments and switch between them). Anyway, I'll make this accepted answer. -- couldnt find "accept" button though #howtoacceptanswer – user1064910 Jan 29 '13 at 01:06
  • There should be a "checkmark" near the top left corner of the answer (where the number and up/down arrows are. Just click that to accept the answer. – David Wasser Jan 29 '13 at 09:59