1

I want close android app

int id= android.os.Process.myPid();
android.os.Process.killProcess(id);

This code kills the Process.

Lea Cohen
  • 7,990
  • 18
  • 73
  • 99
Sujay
  • 601
  • 1
  • 10
  • 19
  • could you further explain `Why?` you want to do that? I'm only asking because it seems that you're trying to do something that is not recommended. – Budius Dec 27 '12 at 10:21
  • You've gotten many different answers to your question. Were any of these what you were looking for? If so, you might want to accept one of them. Or do you still need help? – David Wasser Dec 31 '12 at 13:01

2 Answers2

2

If you always want your app to start with the root activity (the first activity) every time the user launches the app from the home screen or returns to it from the list of recent apps, then add this to the manifest in the <activity> tag for your root activity:

 android:clearTaskOnLaunch="true"

However, if you want to just finish all activities in the current task you can do the following:

Intent = new Intent(this, MyRootActivity.class); // MyRootActivity should be the name of your root (launcher) activity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("exit", "true"); // This tells MyRootActivity that you want to exit

This will cause all activities in the task to be finished and it will create a new instance of MyRootActivity. In MyRootActivity.onCreate() do the following:

if (getIntent().hasExtra("exit")) {
    finish(); // We want to exit the application, so just finish this activity now
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    Still in some cases its redirecting to some resumed activities. – Sujay Jan 01 '13 at 08:01
  • Well, you need to be more specific if you want more help. Please also realize that there is a long-standing Android bug related to launching an application for the first time from an IDE (IntelliJ, Eclipse, etc.) or from the installer. You may be seeing this bug. Make sure that when you put the app on the phone you do NOT launch it from your IDE or the installer. You should always start the app by selecting its icon from the list of available applications. – David Wasser Jan 02 '13 at 09:16
0
    this.finish();
    Intent intent = new Intent(getApplicationContext(), CloseApp.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

This code works for me. This will close all the existing activities as well as when reopen starts from first activity.

Sujay
  • 601
  • 1
  • 10
  • 19