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: