Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit/Finish an app/activity - android

Tags:

I've got 4 activities say Act1, Act2, Act3 and Act4. A button in Act1 opens Act2, a button in Act2 opens Act3, a button in Act3 opens Act4.

I want two things to be done:

  1. I've a button in Act4 which directs the user to Act1, the prob is when the user clicks back in Act1, i want to close the app instead of opening the Act4..

  2. I've option in menu 'exit' in all activities 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.

like image 330
kumareloaded Avatar asked Jun 18 '12 10:06

kumareloaded


People also ask

How do I close an app on an android?

Close one app: Swipe up from the bottom, hold, then let go. Swipe up on the app. Close all apps: Swipe up from the bottom, hold, then let go. Swipe from left to right.

How do you exit an application?

You can call System. exit(); to get out of all the acivities.

How do you finish an activity?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.


1 Answers

Use below code in your Act4'th Menu.xml's exit button -

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

And, in your first activity's onCreate() method just put the below code -

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

This will exit your app.

like image 164
Praveenkumar Avatar answered Oct 10 '22 06:10

Praveenkumar