3

I have three activities: A, B, C. Activity A can start B or C. When A starts C i can return to A just have pressed back button. But i want "return" to B and then to A (with second press of back button).

I tried to use TaskBackStack in this way:

final TaskStackBuilder builder = TaskStackBuilder.create(this)
    .addParentStack(this)
    .addNextIntent(new Intent(this, B.class))
    .addNextIntent(new Intent(this, C.class));
builder.startActivities();

But its not working... Can anyone help me?

Sorry for my English and thanks in advance

Artyom Shalaev
  • 227
  • 1
  • 9

4 Answers4

3

Override onBackPressed method in your activity C.

                @Override
                public void onBackPressed() {
                    super.onBackPressed();
                    //finish();
                        Intent intent = new Intent(ActivityC.this,ActivityB.class);
                        startActivity(intent);
                     }

If you write finish you can go to Activity A because you came from Activity A. That's why use intents. Like that in Activity B also.

@Override
public void onBackPressed() {
     super.onBackPressed();
     //finish();
     Intent intent = new Intent(ActivityB.this,ActivityA.class);
     startActivity(intent);
}

Let me know the status?

chridam
  • 100,957
  • 23
  • 236
  • 235
Veerababu Medisetti
  • 2,745
  • 2
  • 14
  • 11
2

Override your onKeyDown method

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            //Do stuff, like launching a new activity

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143
1

go to this start basic with Activity Lifecycle. you ll have better knowledge of doing this. Go to this Calling one Activity from another in Android. you ll find many examples on google. try it.. one of the example.

Community
  • 1
  • 1
Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121
1

Before try to answer you question I have some comments about your approach to navigate between your activitys. In general, this approach is not a good idea, because doing that, you are not following the Android Navigation Pattern, and this can make your application not user friendly, since most Android Application use the Navigation Pattern.

The Android Navigation Pattern says:

Temporal navigation, or navigation between historical screens, is deeply rooted in the Android system. All Android users expect the Back button to take them to the previous screen, regardless of other state. The set of historical screens is always rooted at the user's Launcher application (the phone's "home" screen). That is, pressing Back enough times should land you back at the Launcher, after which the Back button will do nothing.

However, there is another possiblite to Navigate in your application using The Android Pattern, but I believe that is not a solution for your problem. You can take a look at Up Navigation

To finalize, I have a relevant comment about solution that uses override the OnBackPressed() to Start a new Activity. You should really avoid that, because using it, you will can make crazy your chronologic navigation. Because when you create an activity inside the OnBackPressed, you are always putting a new activity on Android Stack, so there are two problems here:

  1. The stack can grow quickly depending of the user behavior
  2. Can be hard get out of your applcaiton (Can be very hard to make your stack become empty)
Bruno Mateus
  • 1,727
  • 18
  • 25
  • I can create an action named for example "DELEGATE_TO_C" in B and then start it from A. In B I can read intent action and start C. What do u think about this case? – Artyom Shalaev Nov 16 '12 at 06:48
  • Well, I tried make something similar, but did not work perfectly. Let me explain: Instead of use a "specific" intent, I just put a extra boolean in the intent(flag, to indicate if should go to C) and I read it on B's onCreate method. However, after call startActivity, although C activity had shown up and when a pressed back always went to B. I could see the B activity for some miliseconds in the screen before C appears and that is a BAD thing in my opinion. – Bruno Mateus Nov 17 '12 at 19:13