25

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
kumareloaded
  • 3,882
  • 14
  • 41
  • 58
  • [Try this answer](http://stackoverflow.com/a/5140545/940096) – Praveenkumar Jun 18 '12 at 10:45
  • you can find your answer here............. [check out this..........][1] [1]: http://stackoverflow.com/questions/11077020/exiting-an-android-application-on-back-button/11077196#11077196 – Anu Jun 18 '12 at 10:46
  • There was an almost exact duplicate asked sometime ago today [here](http://stackoverflow.com/q/11077020/368070) I would recommend reading [this post](http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238#2034238) to understand why you should not implement what you are trying. – gideon Jun 18 '12 at 10:46
  • here is what you need............ [here][1] [1]: http://stackoverflow.com/questions/11077020/exiting-an-android-application-on-back-button/11077196#11077196 – Anu Jun 18 '12 at 10:48
  • thanks guys will try all those out n get back :) – kumareloaded Jun 18 '12 at 11:08
  • Have you got whatever you need? – Praveenkumar Jun 18 '12 at 11:33

12 Answers12

40

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.

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • Even if you call finish(), your super.onCreate() method will be called regardless. And then Android can reset your whole activity stack if you're not careful. – IgorGanapolsky Oct 25 '12 at 21:17
  • @IgorG. Then, what's the best way to finish this? – Praveenkumar Oct 26 '12 at 04:15
  • 2
    Your way is good. But I'm saying to be careful, that if a super.onDestroy() or super.onCreate() is pending, then those will still get called after you call finish(). And that's when you can have undesired behavior if you have subclassed another activity's onCreate() or onResume(). – IgorGanapolsky Oct 26 '12 at 14:36
  • I am using bound service in my project and this is not closing my app from child activity. – shehzy Jun 25 '15 at 12:00
  • My Act1.class is MainActivity. If I use System.exit in place of finish(); then it is working. – Vempati Satya Suryanarayana Mar 10 '19 at 13:39
10

Check out this link:

Click here

You can use :

@Override
public void onBackPressed()
{
     moveTaskToBack(true);
}

in all activities to close the app.

Community
  • 1
  • 1
Krishna Suthar
  • 3,071
  • 6
  • 31
  • 37
  • Hi, Have you tried this before? Because, it won't exit the app. Its just run the app in background – Praveenkumar Jun 18 '12 at 11:20
  • Ok. I've given one link. Check all answers on that. – Krishna Suthar Jun 18 '12 at 11:26
  • 3
    This doesn't exit the app. It moves the task to the background. If the user then uses long-press on the HOME key and selects this app, it will return to the foreground with the activities still in the task as they were before. – David Wasser Jun 18 '12 at 11:26
  • Best Answer ever for my case that has multi activity, its the only answer that works with me – FarOoOosa Jun 01 '16 at 20:34
5
Intent intent = new Intent(Act4.this, Act1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Clear the flag before you switch back to previous activity. It might help you.

user247702
  • 23,641
  • 15
  • 110
  • 157
Prerak
  • 125
  • 1
  • 5
5

Place this code in your app:

moveTaskToBack(true);
android.os.Process.killProcess(Process.myPid());
System.exit(1);
Unheilig
  • 16,196
  • 193
  • 68
  • 98
Manikumar Gouni
  • 111
  • 3
  • 4
3

finish previous activity when you are go to the next activity means write finish(); after startactivity(intent); and write below code for start first activity from fourth activity's button click event.

Intent in1=new Intent(Act4.this, Act1.class);
startActivity(in1);
finish();

And Write Below Code on Your Exit Button's Click event in all activities.

finish();
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
3

Use android:noHistory = "true" in your AndroidManifest.xml file

    <activity
        android:name=".Act1"
        android:noHistory="true" />
    <activity
        android:name=".Act2"
        android:noHistory="true" />
    <activity
        android:name=".Act3"
        android:noHistory="true" />
    <activity
        android:name=".Act4"
        android:noHistory="true" />
Jithin Jude
  • 840
  • 14
  • 19
0

The approach I use is to start all child activities with startActivityForResult. Then I can putExtra("STATE", some_value) on exiting any child and use the state value to determine what to do up through the parents.

If I want to exit the app from a deep child, "STATE" would be "exit" and each child in the hierarchy would simply get the StringExtra for "exit", do a putExtra("STATE", "exit") and call finish() and then the app would call finish() in the end. You can use this to achieve any desired state.

Using startActivityForResult to pass messages this way avoids having to pass awkward references down into the child activities and simplifies your approach to setting the desired state of the app.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
R Earle Harris
  • 985
  • 9
  • 17
0

If you have another activity behind ( in the application activity stack), you could use finish() to exit the current activity.

However, the back function too suppose to remove the current activity from your application activity stack.

In my experience, it is very unreliable to rely on the activities in the back stack ( in the application activity stack). Because of that I used to exit each activity when I go deeper.

In your case:

Act 1 -> exit -> Act 2 -> exit -> Act 3 -> exit -> Act 4 -> exit -> Act 1 -> exit 

The above method will exit all the activities.

However, if you want to run some code in the background, it is better to rely on a "Service" rather than an "Activity". You can let the "Service" exit after doing its assigned work.

kalan nawarathne
  • 1,944
  • 27
  • 26
0
Do a 
<code>

try
{
finalize();
finish
}
catch(Exception error)
{
error.printStackTrace();
}
//This would exit your App neatly
</code>
0

I use this method :
1. Create static activity in the first activity that will close the remaining activity(s).
2. Then call it in the static method like this, it will close the app from wherever you call it.

public static Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity=this;
}
public static void CloseApp(){
    activity.finish();
} 
Komal12
  • 3,340
  • 4
  • 16
  • 25
Hakim Douib
  • 495
  • 1
  • 6
  • 13
-1

Tray this one

Intent intent =new Intent(MainActivity.this, ClassName.class);
  finish
startActivity(intent);
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
-1

Start Activity1 using intent and try calling finishAffinity() in onBackPressed() of Activity1.

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53