Hesham Saeed
Hesham Saeed

Reputation: 5378

Activities Stack Issue

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?

Upvotes: 4

Views: 213

Answers (7)

A. Binzxxxxxx
A. Binzxxxxxx

Reputation: 2861

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);

Upvotes: 0

M.A.Murali
M.A.Murali

Reputation: 10148

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/

Upvotes: 0

David Wasser
David Wasser

Reputation: 95568

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 ;-)

Upvotes: 1

Khan
Khan

Reputation: 7605

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);
}

Upvotes: 0

Raghu Nagaraju
Raghu Nagaraju

Reputation: 3288

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.

Upvotes: 0

Vinay W
Vinay W

Reputation: 10190

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

Upvotes: 0

Antrromet
Antrromet

Reputation: 15414

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);
    }

Upvotes: 0

Related Questions