-9

How do i finish application? I use below code but is only finish current activity, go back to previous activity. I want to finish activity when button click i try finish(); system.exit(0); but not helping me

    buttonwithleft.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            quit(); 
        }
    });
    public void quit() {
    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);

    System.exit(0);
   }
Nargis
  • 4,687
  • 1
  • 28
  • 45
Nice Guy
  • 11
  • 1
  • 6
  • http://stackoverflow.com/a/17292441/931982 – stinepike Jun 25 '13 at 08:35
  • you should not doing this at all ... http://stackoverflow.com/questions/2042222/close-application-and-launch-home-screen-on-android/2043302#2043302 and http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238#2034238 – Selvin Jun 25 '13 at 08:37

3 Answers3

1

Try this code... I hope it is useful for you.

buttonwithleft.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        quit(); 
    }
});
public void quit() {
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(startMain);
}

that code working for me... and I think also working for you...

Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
0

If you want to finish all activities of your application you should maintain this flow.

Assume i am in Activity D. My actual flow is A->B->C->D.i want to kill my previous activities A,B,C. I used to call my first activity A when i am going from Activity D. So i added intent FLAG_ACTIVITY_CLEAR_TOP to call activity A. So the middle activities will cleared and will show only Activity A. And i am passing some flag value as intent extras to activity A. like this

         Intent intent = new Intent(D.this,A.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("finishstatus", true);
                this.startActivity(intent);
                this.finish();

and i am checking the bundle value in onCreate method of Activity A like

       bundle = this.getIntent().getExtras();
        if(bundle!= null)
        {
       boolean isActivityToBeFinish =  this.getIntent().getExtras().getBoolean("finishstatus");
            if(isActivityToBeFinish)
            {
                finish();
            }
        }

If the status value available and it is true the i am finishing the activity A also. So my problem is cleared and i am successfully finishing the all previous activities.

May be my explanation is not good but the final work is this only.

Raghu Mudem
  • 6,793
  • 13
  • 48
  • 69
0

You can set android:noHistory="true" in your manifest file on each activity, then when you click on your button call the finish(); function. This will exit your application. eg

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="name_of_act"
            android:label="@string/app_name"
            android:noHistory="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="act_name"
            android:label="@string/app_name"
            android:noHistory="true" >
        </activity>
    </application>
Lunchbox
  • 1,538
  • 2
  • 16
  • 42