I'm new to Android development and I have an app with various activities. For performance reasons I'd like to properly manage the activities when users are using my app. Here's my problem.

Activity A - starting activity with map

Activity B - user navigates to Activity B (a list view) from Activity A.

The user then selects the map icon to navigate to Activity A again.

So if you can image it, my activity stack is now:

  1. Activity A
  2. Activity B
  3. Activity A

So if I press the back button the device it will take forever as it scrolls through activities.

Is there a way of managing this so the old activity is destroyed and is just re-created upon choosing an activity nav icon. I've read up about onDestroy() and onStop() but I'm a little confused at their implementation.

Apologies for a poorly worded question but I'm unsure of the correct lexicon to ask about activities.

有帮助吗?

解决方案

One simple solution is to kill the Activities as soon as they leave the foreground.

You could do that by calling finish() inside onPause().

其他提示

You could have B finish itself and return to A instead of starting another A. Or, if your stack might be more complicated, like this:

D
C
B
A

D could start A with FLAG_ACTIVITY_CLEAR_TOP, which would cause D, C, and B to be finished, leaving A on top. (That intent flag interacts non-intuitively with a couple other flags, so read the docs.)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top