R KiranKumar
R KiranKumar

Reputation: 835

force-stop the application programmatically in Android

I want to force-stop an application from my Android app, (Instead of doing manually like apps-force->stop). How to achieve this?

I used:

android.os.Process.killProcess(android.os.Process.myPid());
system.exit(0);

It crashes.

Upvotes: 4

Views: 13751

Answers (3)

Silambarasan Poonguti
Silambarasan Poonguti

Reputation: 9442

try this...

private void QuitApp() {

    //getActivity().moveTaskToBack(true); //Move the task containing this activity to the back of the activity stack. 

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);
    System.exit(0);
  }

Upvotes: -2

Mahesh
Mahesh

Reputation: 1589

Just put this code in your close button and your app will stop

private void endapp() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
    android.os.Process.killProcess(android.os.Process.myPid());
}

You can try with this one also

android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);

Upvotes: 2

Sekar
Sekar

Reputation: 1071

android.os.Process.killProcess(android.os.Process.myPid()); this code is correct and best one. If you need more information Refer link as How to close Android application? and Process.

You can also try this code

Intent intent = new Intent(yourCurrentActivity.this, yourNextActivity.class);  
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);  
android.os.Process.killProcess(android.os.Process.myPid());

You start an activity, and then close current activity.

Upvotes: 3

Related Questions