问题
I want my activity, which shows a dialogue, to finish when a user clicks on the positive button of the dialogue. Is this possible. where do I place finish()?
Code that calls the dialogue:
if(name.equals("")) {
DialogFragment newFragment = new NouserFragment();
newFragment.show(getFragmentManager(), "makeprofile"); }
code for dialogue:
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.nouseralert)
.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(getActivity(), Editprofile.class);
startActivityForResult(intent, 0);
}
})
.setNegativeButton(R.string.ignore, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
回答1:
Okay. I was able to finish the activity by putting getActivity().finish()
under the onClick()
of dialogue interface.
回答2:
you can use this code:
public void showMessageAlert(){
runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(HomeScreen.this);
builder.setTitle(ConfigClass.ALERT_TITLE);
builder.setMessage(ConfigClass.MSG_ALERT);
builder.setCancelable(true);
builder.setInverseBackgroundForced(true);
builder.setPositiveButton("I Accept", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
ActivityName.this.finish();
}
});
builder.setNegativeButton("I decline", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//Do nothing
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
回答3:
on click of positive button of the dialog call dialog.dismiss();
then finish();
回答4:
That's a Java problem with inner class visibility/scope. Inside the listener, this
refers to the OnClickListener
object and (luckily) it has no finish()
method. To have it refer to the activity, either:
- use simply
finish()
, without any prefix, if the listener is defined inside the activity. This way Java will look for the nearest enclosing object which defines a method namedfinish()
, and it will find the Activity's one - use
YouActivity.this.finish()
to refer to the enclosing activity instance without any ambiguity (obviously this holds true if the listener is a (possibly anonymous) inner class ofYourActivity
- Call
mActivity.finish()
on some instance of your activity if it's defined in another file altogether (and maybe you'll have to cast theContext
object)
Usually listeners are defined as anonymous inner classes inside activities, so calling finish() unprefixed should be enough, but you may want to use the A.this.finish()
form if there are name collisions
来源:https://stackoverflow.com/questions/14039341/finish-an-activity-when-user-presses-positive-button-on-an-associated-dialogue