Jesús Ayala Jesús Ayala - 1 year ago 62
Android Question

How to exit from Android App even if current Activity is not Main Activity

I have this activity flow

HomeActivity > Activity1 > Activity2 > Activity3 > HomeActivity

I switch back directly from Activity3 to HomeActivity by pressing cancel button on Activity3.

but when I press back button on HomeActivity I want to exit from the app, however it takes me back to Activity3... how to exit the app??? this is my code
on Home activity:

public void onBackPressed() {

if I press back button when App is just started on HomeActivity it works normally, closing the app...

How to make it work when I navigate through all these other activities?

Answer Source

You should switch from Activity3 to HomeActivity this way:

Intent launchNextActivity = new Intent(Activity3.this, HomeActivity.class);

Then onBackPressed() should work fine.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download