4

I have two sets of Activities suppose 3 Activities each set, (A1,B1,C1 || A2,B2,C2) I start my App from A1 then -> B1 -> C1 here I want to jump from C1 to -> A2 and at A2 if I press back it should exist the App and not put me back for C1, then from A2 I navigate to -> B2 -> C2.

So it is basically I want to change the starting Activity, it is like I have two Apps in one App and when I flip to the second App I have to clear the Activity Stack. Is that possible? Any Ideas?

Hesham Saeed
  • 5,358
  • 7
  • 37
  • 57

7 Answers7

1

Seems to me you've answered your own question. You wrote:

So it is basically I want to change the starting Activity, it is like I have two Apps in one App and when I flip to the second App I have to clear the Activity Stack.

I would do it this way:

Create a DispatcherActivity which is the activity that gets launched when the application is started. This Activity is the root activity of your task and is responsible for launching either A1 or A2 depending... and NOT call finish() on itself (ie: it will be covered by A1 or A2 but still be at the root of the activity stack).

In A1, trap the "back" key and tell the DispatcherActivity to quit like this:

@Override
public void onBackPressed() {
    Intent intent = new Intent(this, DispatcherActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addExtra("exit", "true");
    startActivity(intent);
}

This will clear the task stack down to the root activity (DispatcherActivity) and then start the DispatcherActivity again with this intent.

In C1, to launch A2, do the following:

Intent intent = new Intent(this, DispatcherActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addExtra("A2", "true");
startActivity(intent);

This will clear the task stack down to the root activity (DispatcherActivity) and then start the DispatcherActivity again with this intent.

In DispatcherActivity, in onCreate() you need to determine what to do based on the extras in the intent, like this:

Intent intent = getIntent();
if (intent.hasExtra("exit")) {
    // User wants to exit
    finish();
} else if (intent.hasExtra("A2")) {
    // User wants to launch A2
    Intent a2Intent = new Intent(this, A2.class);
    startActivity(a2Intent);
} else {
    // Default behaviour is to launch A1
    Intent a1Intent = new Intent(this, A1.class);
    startActivity(a1Intent);
}

In A2, trap the "back" key and tell the DispatcherActivity to quit using the same override of onBackPressed() as in A1.

Note: I just typed this code in, so I've not compiled it and it may not be perfect. Your mileage may vary ;-)

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • +1 for your efforts, need time to check it, but does this works also if I decided at B1 before reaching C1 to jump for A2? – Hesham Saeed Jul 11 '12 at 16:07
  • Yes, of course. At B1 you just send the Intent to the DispatcherActivity and tell it to start "A2". That's why you should do it this way. It is very flexible and you don't need much extra code. – David Wasser Jul 11 '12 at 16:28
  • How can I implement this if I have Splash screen for the App which it has to be shown once in the beginning? because I have to go for SplashActivity first before going DispatcherActivity? or there is another workaround? thanks – Hesham Saeed Aug 07 '12 at 14:11
  • Not a problem. The Splash screen is the first activity you start (ie: it has `action.MAIN` and `category.LAUNCHER` in the manifest). It does what it needs to do then it starts the `DispatcherActivity` and calls `finish()` on itself. Now only the `DispatcherActivity` is in the activity stack. The rest works as I've described it. – David Wasser Aug 07 '12 at 15:28
0

if you want to close your app when press the back button instead of go back last activity, you should overwrite the back button. in the overwrite method call finish() method after close the activity. i hope this may help you.

edited:

check the below link : this is for close all activities in your view stack. before closing your app close all activities.

http://www.coderzheaven.com/2011/08/27/how-to-close-all-activities-in-your-view-stack-in-android/

M.A.Murali
  • 9,988
  • 36
  • 105
  • 182
  • This isn't right. Using `noHistory` will prevent the user from normally navigating from A1->B1->C1 and from A2->B2->C2. Wrong approach. – David Wasser Jul 11 '12 at 15:00
0

You can check when the button that is pressed in Activity A2 and then if its a Back button you can close the app. You can use the following method in A2

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            this.finish();
        }
        return super.onKeyDown(keyCode, event);
    }
Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • This doesn't work. Calling `finish()` in A2 won't close the app, it will just finish A2 and then go back to the previous activity in the task (which would be C1) – David Wasser Jul 11 '12 at 14:58
  • @DavidWasser You can close all the activities in that onKeyDown() method so that the app closes. http://stackoverflow.com/questions/5453206/how-to-close-all-the-activities-of-my-application – Antrromet Jul 12 '12 at 04:50
0

try launching the activity A2 with this intent - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Vinay W
  • 9,912
  • 8
  • 41
  • 47
0

Handle this using onBackPressed and finish methods.

Before launching other activity, better you close the current activity using finish() method.

If you want to go to previous activity on back press, override onBackPressed method and call the particular intent.

In A2 activity, add finish method in onBackPressed method (dont call previous activity here). This is one of the way.

Raghu Nagaraju
  • 3,278
  • 1
  • 18
  • 25
0

while passing Intent from Activity C1 to A2 use as

Intent intent=new Intent(C1.this,A2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

and in Your Activity A2 Override Back Button by

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        A2.this.finish();
    }
    return super.onKeyDown(keyCode, event);
}
Khan
  • 7,585
  • 3
  • 27
  • 44
  • This doesn't work. For this to work A2 would already have to be in the activity stack (at the root), which it isn't. – David Wasser Jul 11 '12 at 14:56
0

FLAG_ACTIVITY_CLEAR_TOP is not what you are looking for. i think you are looking for:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
A. Binzxxxxxx
  • 2,812
  • 1
  • 21
  • 33