dev_android
dev_android

Reputation: 8818

Android back problem

I have three activities, from A it is going to B, from B it is going to C. I am using following code to transfer from one activity to another.

Intent intent = new Intent().setClass(this, B.class);
startActivity(intent);

I want that when I use the back button, it should come to B if it is at C(which is ok for me), but if I use back button at B activity, it should not go to A, it should directly go out the application. How it can be arranged?

Upvotes: 0

Views: 415

Answers (4)

DanO
DanO

Reputation: 10270

In class A you would put:

Intent intent = new Intent(this, B.class);
startActivity(intent);
finish();

This will remove class A from the Activity stack.

Upvotes: 0

Thorben
Thorben

Reputation: 6981

There you go

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            this.finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

Upvotes: 3

Pim Reijersen
Pim Reijersen

Reputation: 1133

Override the member function onBackPressed() inside your Activity class.

Example:

public void onBackPressed() {
    Intent intent = new Intent().setClass(this, B.class);
    startActivity(intent);
}

Upvotes: 1

olamotte
olamotte

Reputation: 917

call finish(); when you launch the activity B from the activiy A

Upvotes: 2

Related Questions