I'm opening an Activity
using this:
startActivity(new Intent(Parent.this, Child.class));
And on the child, I have this code on the onCreate
function (the if
contains more than just true
, of course):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (true) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton("OK", null);
builder.setTitle("Error");
builder.setMessage("Connection error, please try later.")
.show();
finishActivity(0);
return;
}
}
Why is the activity not closing? I get the alert box, but then I have to tap the "back" button to go back.
Try using the finish() method to close the Activity.
do this in the on create
if (true) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton("OK", null)
.setTitle("Error")
.setMessage("Connection error, please try later.")
.setCancelable(false)
.setPositiveButton("_Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.show();
return;
}
and in your AndroidManifest.xml do the following:
<activity class="MyDialogActivity" android:theme="@android:style/Theme.Dialog"/>
Now you Activity will start and show the Dialog. It feels like there is only the dialog for the user. There is an activity displayed, but it is behind the dialog. So the effect is okay. Otherwise you can create the Dialog in the activity itself (setcontentview).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With