How to close an activity (remove from Foreground)?

1

I have the following code in my MainActivity class that has an AlertDialog and the onClick () method handling:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;

public class MainActivity extends Activity implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Você deseja sair da aplicação?")
                .setPositiveButton("OK", this)
                .setNegativeButton("Cancelar", this);
        Dialog dialog = builder.create();
        dialog.show();
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
        case DialogInterface.BUTTON_POSITIVE:
            dialog.cancel();
            finish();
            break;
        case DialogInterface.BUTTON_NEGATIVE:
            break;
        default:
            break;
        }

    }
}

Manifest:

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="20" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

I want to completely close the activity without being in Foreground, but the application still follows the image below when I press the button that is marked red:

    
asked by anonymous 27.09.2014 / 08:16

2 answers

1

This area you're showing in the image is Recents Screen . When an application appears there, it is not necessarily in foreground, but it has recently been used, possibly closed (as is your case).

You can control the appearance of your application on Recents Screen by using the android:excludeFromRecents attribute in the Activity Launcher declaration. As a result, your application will never appear on Recents Screen , nor when it goes to the background by the Home button.

To use, just set it to its Activity :

<activity
    android:name=".MainActivity"
    android:label="@string/title_activity_main"
    android:excludeFromRecents="true">

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

For more details, just visit the documentation for this attribute .

    
27.09.2014 / 21:39
0

In this code the activity will be closed as soon as you leave it or open another activity

Override Activity's onPause () method

protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}
    
27.09.2014 / 09:44