0

I have two activities. Activity A starts activity B when button is pressed. Activity B loads some data on create. When I press back button activity B is being destroyed but I want to just pause it and get back to activity A. I tried:

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}

But it gets me to the home screen, not activity A.

micnyk
  • 726
  • 8
  • 27
  • 1
    call `finish()` in activity B – FoamyGuy May 09 '13 at 16:09
  • @FoamyGuy calling `finish()` in activity B is exactly what the default behaviour of the BACK button does. ActiviyB will be destroyed. This isn't what OP wants to do. – David Wasser May 09 '13 at 22:22
  • I've added an answer, but I think your design is flawed. If ActivityB loads some data and you want to keep that data around then you should either store the data in static variables somewhere or you should load that data using a `Service` which can then be accessed by any of your activities. Trying to "keep the activity around" because it has loaded data is going to make your app design difficult to manage, maintain and understand. – David Wasser May 09 '13 at 22:34

1 Answers1

4

If you just want to bring ActivityA to the front, do this:

@Override
public void onBackPressed() {
    Intent intent = new Intent(this, ActivityA.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(intent);
}

This will rearrange the activity stack so that A will be on top (showing) and B will be behind it. Now when the user clicks BACK in ActivityA, the default behaviour will be to finish ActivityA and return to ActivityB.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Dear David, this is probably not working (see http://stackoverflow.com/questions/11453724/bring-my-activity-back-to-the-top). In fact a new activity is created and put onto the stack. – Trinimon May 10 '13 at 08:49
  • @Trinimon the linked question refers to bringing another **task** to the front, not reordering **activities within the same task**. These are 2 completely unrelated things. When you say _In fact a new activity is created and put onto the stack_, have you actually tried this in OP's configuration? – David Wasser May 10 '13 at 09:08
  • Yes, I created a small demo app with two activities lets say `A` and `B`. `A` called `B` with `FLAG_ACTIVITY_NEW_TASK`, `B` called `A` with `FLAG_ACTIVITY_REORDER_TO_FRONT`. It created a new activity and pushed that on top of the stack. So that is in line with the post I mentioned. May be there is just something missing? – Trinimon May 10 '13 at 09:37
  • @Trinimon `REORDER_TO_FRONT` only works on activities within the same task. If you've actually started ActivityB in a **new task** then this will not work. However, that's not standard behaviour. Normally an application keeps its activities all in the same task. What you are seeing is a conflict between `FLAG_ACTIVITY_NEW_TASK` and `FLAG_ACTIVITY_REORDER_TO_FRONT`. OP didn't say anything about having **multiple tasks**, and because the standard behaviour would be to have ActivityA and ActivityB in the same task I assume he doesn't have multiple tasks. – David Wasser May 10 '13 at 11:37
  • Ok, I see. Finally it worked. Meanwhile I did some experiments with `FLAG_ACTIVITY_BROUGHT_TO_FRONT` too, what sounds similar though it seems to be another thing. So I took always a wrong combination before. Using only flag 'REORDER_TO_FRONT' for starting activity B it worked (none for activity A). Thanks! – Trinimon May 10 '13 at 12:06
  • @Trinimon `FLAG_ACTIVITY_BROUGHT_TO_FRONT` is not something that you should set yourself. Setting it yourself does nothing. It is set by Android when Android brings a **task** (not an activity) to the foreground, if the activity being started has `launchMode="singleTask"`. See the description of `launchMode="singleTask"` at http://developer.android.com/reference/android/R.styleable.html#AndroidManifestActivity_launchMode – David Wasser May 10 '13 at 12:12