How to close an Activity(remove from Foreground)?

Asked

Viewed 7,960 times

1

I have the following code in my Mainactivity class that has a Alertdialog and processing in the onClick method():

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 close Activity completely without staying in the Foreground, but the application still follows the image below when I press the button marked red:
Foreground

  • I didn’t understand the doubt: Without staying in the foreground with the Dialog visible? Or after selecting the option to leave? The finish no longer does that?

  • @Wakim does not dialog is closed but Activity is still on foreground...

  • He even debugged the method onClick, he enters the case of BUTTON_POSITIVE and does not end? If so, it could include the statement of Activity in the Manifest?

  • it enters yes in the case BUTTON_POSITIVE but it does not finish... it is still in the foregroud.. and precisely that the problem...

  • Could include the statement of that Activity in the Manifest? Could also check if any error occurs in the methods onPause, onStop, etc? When an error occurs in life cycle methods Android restarts to Activity.

  • this Activity is already the mainActivity this statement... I will take a look at these methods..

  • I edited my post.. I took the part of my project that I want to test and made another project containing only this Activity.. keeps giving the same thing stays in the foreground

  • Here the code worked (copied and pasted), I would like to know which attributes defined in the tag <activity> for that Activity. I can’t imagine what the problem might be.

  • I’ll edit my post... I’ll insert an image...

Show 4 more comments

2 answers

1


This area that is showing in the image is the Recents Screen. When an app appears there, it is not necessarily on foreground, but it was recently used, possibly closed (as is your case).

You can control the appearance of your app on Recents Screen, simply use the attribute android:excludeFromRecents in the statement of Activity Launcher. With this, your app will never appear on Recents Screen, nor when going to the background by the button Home.

To use just set it in your 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 go to documentation of this attribute.

  • was just that. thank you

0

in this code the Activity will be closed as soon as you exit it or open another Activity

Overwrite the Activity onPause() method

protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}
  • does not work.. the dialogue is executed when the Initiative is opened.. ie the dialog remains and Activity and closed... I just want to close only when I press to leave..

  • has nothing to do with what I need at the moment to solve the problem...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.