views:

465

answers:

1

I'm writing a simple Android app, and I'd like better control over the navigation/relationship between the activities. I don't want my activities to act like android activities...I don't want them to stack up within the Task. I want one activity (let's call it MainActivity) to be the landing point and always be at the bottom of the stack, and I want only one instance of my second activity (call it SecondActivity) to be above it in the stack...would be nice to reuse it as well. I thought I could get this behavior by making MainActivity be the "main" activity, and declare them both as launchMode=singleTop. This isn't working at all. I provide navigation between them using menus, so when I go back and forth a bunch of times and back out of the app, I go through the whole stack.

How's the best way to have fine control over the Task's Activity stack? I want MainActivity to always back out of the app, and SecondActivity to always back into a single instance of MainActivity. As well, I'd love to get singleTop working so I would use onNewIntent instead of creating and destroying every time. Using the manifest as well as the intent flag is just not working. Any ideas?

+1  A: 

Well, you could always just call "finish()" within whatever Activity is calling another activity after the "startActivity()" call. I would definitely advise against trying to stuff an entire app into two activity classes and try to swap views based on what they're doing. If it's that important to you, just close your activities as you launch new ones (obviously not the MainActivity, though).

MattC
It's not a matter of stuffing an entire app into two activities, as it's a relatively simple app that really only has two different views, one of which is just a landing page. And, I'm looking to get the reuse that singleTop launchMode advertises. I don't know exactly how much goes into constructing an Activity class in Android, but I know in other platforms I'd prefer reusing a singleton instance where it made sense over constructing and destructing the same thing over and over again
Rich
I would describe that as premature optimization. There are 10,000+ apps on the Android Market. The vast majority will simply start new activities when needed and let those activities close up when the user exits them. After all, the memory management system behind Android assumes that model, the core Android development team promotes that model, etc. Remember: your activities get destroyed and recreated by default on an orientation change.
CommonsWare
good point...thanks
Rich