Finish an activity when user presses positive button on an associated dialogue

浪尽此生 提交于 2019-12-11 15:09:21

问题


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 named finish(), 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 of YourActivity
  • Call mActivity.finish() on some instance of your activity if it's defined in another file altogether (and maybe you'll have to cast the Context 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!