user2590541
user2590541

Reputation: 512

Closing Application and killing it from ram

hi I want to close my application and also want kill it from ram by clicking button. i am using this method, but it just minimize application and didn't close complete app. what to do ?

public void AppExit()
{

    this.finish();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    }

Upvotes: 0

Views: 632

Answers (3)

t0mm13b
t0mm13b

Reputation: 34592

Calling:

  • System.exit(0);
  • Process.killProcess(android.os.Process.myPid());

Both of which are definitely not a good idea!

Instead use the activity's finish(); method - let Android handle the cleanup of RAM management, which it is very good in doing so!

If you do not want your activity to be shown in the activity stack, the flags, Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP would usually suffice for launching another activity.

OR specify in your AndroidManifest.xml in your activity tag, android:noHistory="true".

Upvotes: 4

m0skit0
m0skit0

Reputation: 25873

System.exit will exit the VM and Android will kill this process.

Activity#finish() is usually the popular choice, but this does not need to be executed right away and also only affects this particular Activity, not the whole process.

Just FYI: killing apps manually is not a good app design in Android (except maybe some special situations). If you could explain why you want to kill your app, we can probably tell you a better solution.

Upvotes: 1

mdDroid
mdDroid

Reputation: 3195

use single line code

System.exit(1);

or

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

Upvotes: 0

Related Questions