user3240604
user3240604

Reputation: 417

Android back button in device

I have Activity A (Main) and Activity B. From A, I can go to B. When I test I do the next:

Go from A (a1) to B (b1). Then go back to A (a1). The again from A (a1) to B (b2) and go back to A (a1).

If I push the back button again after doing what I said, I want to exit the app but it returns to the first instance of B (b1), and then if I push again, it goes to the first instance of A(a1) and if a push again now it exit the app.

I don't want this behavior, if I am in activity A and push back button I want to exit the app, not to go to every instance of activities until goes to the first one and then exit.

I hope I was clear.

Upvotes: 0

Views: 44

Answers (1)

Fidel Montesino
Fidel Montesino

Reputation: 337

Probably what you are doing is to stack one Activity in top of the other. You can make sure to close B activity if you implement the onBackPressed() function and finish the activity like this:

@Override
public void onBackPressed() {
    finish();
}

This way you'll return to your original A activity (and not another instance -- like A1), and if you try to go back from A you'll exit the app.

I hope it helps!

Upvotes: 1

Related Questions