5

I have an app that circles around the main activity (a main menu). In each other app there is an option menu item that directs to this activity.

At first, I always started a new main activity when this item was selected. Using the intent bundle, I did tell the main activity that some initializations I do on a fresh start were not necessary.

However, I didn't quite like the overall behavior. I stumbled upon android:launchMode="SingleTask" and this seemed to help: now I don't recreate my main menu activity all the time; also, if I press the "back" button I come back straight to the home screen. This feels quite nicely like a proper "main" menu.

My problem now is this: if I run another activity of my app, press home button and then reopen my app (e.g. using "last apps"), then I don't go back to the last activity, but to the main one. The other activity is destroyed.

Any ideas how I can implement the behavior of SingleTask without only being able to return to one activity?

inazaruk
  • 74,247
  • 24
  • 188
  • 156
jellyfish
  • 7,868
  • 11
  • 37
  • 49
  • Glad you found a fix for your issue. For future readers, I think the root cause here was not related to what the accepted answer states, but actually the "bug" explained here: http://stackoverflow.com/questions/2417468/android-bug-in-launchmode-singletask-activity-stack-not-preserved – eselk Jun 22 '15 at 23:10

3 Answers3

3

If your other activities are declared normally with activity defaults in Android, then going back to your app should take you to the same activity where you left off (using the hardware home button)

However remember that the Android system kills applications when it requires system resources. So your app may have been killed when you went to the other application. Then when you get back to your app, the default launcher activity will be restarted, which is your Menu activity.

To get back to the main activity from any activity, do this:

public static void goHome(Context context) {
        final Intent intent = new Intent(context, HomeActivity.class); //give name of your main activity class here
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

That will clear the activity stack and get you back to your main activity. As you declared singleTop, it will bring the existing main activity to the foreground. The flag Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all activities in the stack on top of the main activity. (I am assuming you are within the same application).

Now, all your other activities only need to include a button whose click listener invokes the method goHome();

From your main activity, if you press the hardware back button, it should exit your app.

codinguser
  • 5,562
  • 2
  • 33
  • 40
  • 1
    I would actually like to have normal behavior. However, if I declare my main activity single task, the other activities get destroyed if I press my home button. What I would like to achieve is a) a quick link to the main activity via the menu, without recreating the main app and b) "closing" the whole app by pressing the back button in the main activity. (closing meaning same effect as home button.) However, b) is only a nice to have, so I mainly would like to achieve a). :-) – jellyfish May 16 '11 at 13:31
  • Now I understand what you mean. See my edited answer above. Always make it clear whether you are talking about the hardware back button or a software one. – codinguser May 16 '11 at 14:07
  • Thank you SO much! That was exactly what I was looking for. I also will remember your advice in the future. :-) – jellyfish May 16 '11 at 14:20
0

Why not call finish() on the activities that were created by the main activity? This way you return to the main activity, without creating a new one...

Daniel Novak
  • 2,746
  • 3
  • 28
  • 37
  • sounds good, but then I sometimes have more than one activity between my actual one and my main activity. Is there a way I could close all in between? – jellyfish May 16 '11 at 13:16
  • There may be a better solution, but you could close the activities by calling finish() when onActivityResult gets called. You would set some result flag on the top activity like "CLOSE" and the underlying activities would handle the closing. It would go like this, A creates B which creates C. Now C calls finish with result "CLOSE", B gets the result code in onActivityResult and closes itself by calling finish.... – Daniel Novak May 16 '11 at 13:29
0

I think you should save the state of you activity before starting another activity, and then resume your activity whenever you come back on last activity. see Activity Life cycle from Android http://developer.android.com/guide/topics/fundamentals/activities.html

N. Nasim
  • 1
  • 1
  • 3
  • I would gladly do this, but I don't know how to resume my main activity without going back through all the activity stack. – jellyfish May 16 '11 at 13:17