yashhy
yashhy

Reputation: 3086

Exit android application

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

and use this method

moveTaskToBack(true);

I'm developing an application and at the last activity I need to exit it, I have used the above methods which exits the application the problem is it exists the ONLY the activity and the application runs in background (seen in the task manager). Whenever I load the application again it starts with where I exit it (the last activity).

is there any code to exit it completely and also remove form background(task manager).

Thanks in advance.

Upvotes: 3

Views: 6174

Answers (9)

Daniel Satya
Daniel Satya

Reputation: 37

here is my working code

on your exit button:

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
mainIntent.putExtra("close", true);
startActivity(mainIntent);
finish();

that code is to close any other activity and bring MainActivity on top now on your MainActivity:

if( getIntent().getBooleanExtra("close", false)){
    finish();
}

Upvotes: 0

Ashish
Ashish

Reputation: 31

Try to use android:noHistory flag in your AndroidManifest.xml.

http://developer.android.com/guide/topics/manifest/activity-element.html

Upvotes: 0

lukasz
lukasz

Reputation: 3131

Start your last activity with this code to clear all the activity stack. When you will press the back button or call finish() it will exit the activity.

Intent intent = new Intent(this, YourActivityD.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Be aware that FLAG_ACTIVITY_CLEAR_TASK is only available from API 11
For more info, see: http://developer.android.com/guide/components/tasks-and-back-stack.html

Upvotes: 1

David Wasser
David Wasser

Reputation: 95543

To exit your application, return to your root activity (the activity that is the first one the launcher starts) this way:

Intent intent = new Intent(this, MyRootActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addExtra("exit", "true");
startActivity(intent);

This will clear the task stack down to the root activity and then start the root activity again with this intent.

In your root activity, in onCreate() you need to determine wether to exit based on the extras in the intent, like this:

Intent intent = getIntent();
if (intent.hasExtra("exit")) {
    // User wants to exit
    finish();
}

You also say something about removing your app from the task manager. If you are referring to the "list of recent apps" this isn't a list of the "running applications", it is a list of the applications that the user recently used. Just because your app shows up there does not mean that it is running.

If you really want your app not to show up in the "list of recent apps", just add this to the manifest entry for your root activity:

android:excludeFromRecents="true"

Upvotes: 5

Priya
Priya

Reputation: 1803

you have to finish the activity by calling

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

or You can call the destroy method in your activity

   @Override     
   public void onDestroy()
    {
      super.onDestroy();
      finish();
     }

Upvotes: 0

m0zgen
m0zgen

Reputation: 563

Try this:

Intent finishapp = new Intent(Intent.ACTION_MAIN);
finishapp.addCategory(Intent.CATEGORY_HOME);
finishapp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(finishapp);

Upvotes: 0

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

If you don't want to call finish(); then you can also add

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

with

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

Upvotes: 1

Royi
Royi

Reputation: 755

When you call startActivity(intent); add finish(); after it to close the old activity (unless you want to come back to that activity in the future)

so you code will look like:

startActivity(intent);  
finish();  

Upvotes: 0

EvZ
EvZ

Reputation: 12169

Why you simply not use

this.finish()

?

Upvotes: 1

Related Questions