The Android architecture is not ready to exit an Application with one line of code. You just cannot do it. Rely on your hardware buttons or make a finish()
waterfall effect on all your Activities
. You can use startActivityForResult()
to start your Activities (all of them if you want this method to work). Then, when you want to exit your App, just call setResult(Activity.USER_CANCELED);
and finish();
right after it. It will return to your previous Activity
onActivityResult()
callback. There, if the requestcode
is the right one and the resultCode
is equal to Activity.USER_CANCELED
, just do the same: Call setResult(Activity.USER_CANCELED);
and finish();
. Once more, it will take you back to the previous Activity
, if it exists. And so on, until you exit your app.