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:

I didn’t understand the doubt: Without staying in the foreground with the Dialog visible? Or after selecting the option to leave? The
finishno longer does that?– Wakim
@Wakim does not dialog is closed but Activity is still on foreground...
– Pedro Rangel
He even debugged the method
onClick, he enters the case ofBUTTON_POSITIVEand does not end? If so, it could include the statement ofActivityin theManifest?– Wakim
it enters yes in the case BUTTON_POSITIVE but it does not finish... it is still in the foregroud.. and precisely that the problem...
– Pedro Rangel
Could include the statement of that
Activityin theManifest? Could also check if any error occurs in the methodsonPause,onStop, etc? When an error occurs in life cycle methods Android restarts toActivity.– Wakim
this Activity is already the mainActivity this statement... I will take a look at these methods..
– Pedro Rangel
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
– Pedro Rangel
Here the code worked (copied and pasted), I would like to know which attributes defined in the tag
<activity>for thatActivity. I can’t imagine what the problem might be.– Wakim
I’ll edit my post... I’ll insert an image...
– Pedro Rangel