0

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();
}
krtkush
  • 1,378
  • 4
  • 23
  • 46
  • 3
    Put finish() on click event of positive button of alert dialog. – Dipak Keshariya Dec 26 '12 at 10:36
  • 1
    post your code, so that it will be easy to understand – Ram kiran Pachigolla Dec 26 '12 at 10:36
  • @DipakKeshariya no. That will not work because finish() is only recognized under a activity class. I get this error if I do put finish() on click event. `The method finish() is undefined for the type new DialogInterface.OnClickListener(){}` @Ramkiran sure. – krtkush Dec 26 '12 at 10:38
  • @K_K Then Write YourActivityName.this.finish(); it will solve your problem. – Dipak Keshariya Dec 26 '12 at 10:40
  • @K_K Why are you open new activity on click of positive button? – Dipak Keshariya Dec 26 '12 at 10:42
  • @DipakKeshariya that too does not work. I get this: `No enclosing instance of the type MainActivity is accessible in scope` Maybe I'm doing something wrong? [this](http://stackoverflow.com/questions/14038436/how-to-use-startactivityforresult-through-a-dialogfragment) is my actual problem. And closing the current activity seems like the only way to me. – krtkush Dec 26 '12 at 10:44

4 Answers4

3

Okay. I was able to finish the activity by putting getActivity().finish() under the onClick() of dialogue interface.

krtkush
  • 1,378
  • 4
  • 23
  • 46
1

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();
            }
        });
}
Sam-In-TechValens
  • 2,501
  • 4
  • 34
  • 67
0

on click of positive button of the dialog call dialog.dismiss(); then finish();

david.s
  • 11,283
  • 6
  • 50
  • 82
Pankaj Singh
  • 2,241
  • 2
  • 16
  • 16
0

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

Raffaele
  • 20,627
  • 6
  • 47
  • 86