micnyk
micnyk

Reputation: 746

Pause activity and go back to previous activity

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.

Upvotes: 0

Views: 2854

Answers (1)

David Wasser
David Wasser

Reputation: 95578

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.

Upvotes: 4

Related Questions