简体   繁体   中英

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM