3

I want to quit application in Android. Just put a "quit" button, which kills my app.

I know I shouldn't do this. I know that this is not the philosophy of the OS.

If you know how it can be done, pls share.

In the app, I have many opened activities, so "finish()" will not do the job.

Thank you for your information in advance. Danail

Danail
  • 10,443
  • 12
  • 54
  • 76

3 Answers3

6

Way One

android.os.Process.killProcess(android.os.Process.myPid())  

Way Two

System.exit(0);  
Pentium10
  • 204,586
  • 122
  • 423
  • 502
5

Your answer helped me, Pentium10, but I needed to do one more thing :

I had to clear(close) all my previous activities with

Intent i = new Intent();

i.setClass(this, FirstActivity.class);

i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(i);

This means: every activity, started after FirstActivity, is closed. (In other words, this cleans the activity stack above FirstActivity). Then all I have to do is finish my FirstActivity.

Community
  • 1
  • 1
Danail
  • 10,443
  • 12
  • 54
  • 76
1

Close all the previous activities as follows:

    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("Exit me", true);
    startActivity(intent);
    finish();

Then in MainActivity onCreate() method add this to finish the MainActivity

    setContentView(R.layout.main_layout);

    if( getIntent().getBooleanExtra("Exit me", false)){
        finish();
        return; // add this to prevent from doing unnecessary stuffs
    }
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103