Make simple exit button in android studio

Опубликовано: 16 Октябрь 2024
на канале: Ramy Wahid
14,038
72

The Problem:

I've got 8 activities say Act1,2,...., to Act8.
A button in Act1 opens Act2, a button in Act2 opens Act3,and so on.

In Act8 i have button 'exit' when the user choose it, i want to close the app instead of going back to previous activity.

Tried using finish(); but it didn't meet my requirements.

The Fix :
So to fix that i used this code

in Act8

Intent intent = new Intent(Act4.this, Act1.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

in Act1's onCreate() method just put this code

if (getIntent().getBooleanExtra("EXIT", false))
{
finish();
}

And This Should exit your app.





"Music: « slow motion » from Bensound.com"