Search code examples
javaandroidactivity-finish

Activity does not close when calling finish() in alert dialog


I looked at previous Stack Overflow answers pertaining to this issue and none of them have addressed the issue or someone figured it out but didn't post back what they did.

Here's a typical snooze button I have:

        //Add snooze button
        final Button snooze_button = (Button) findViewById(R.id.snooze_button);

        // Strings to Show In Dialog with Radio Buttons
        final CharSequence[] items = {"15 minutes","30 minutes","45 minutes","1 hour"};

        snooze_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                AlertDialog.Builder builder=new AlertDialog.Builder(context);
                builder.setTitle("Snooze?");
                builder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        final String ONE_TIME = "onetime";
                        AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

                        if("15 minutes".equals(items[which]))
                        {
                            intent.putExtra(ONE_TIME, Boolean.TRUE);
                            PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
                            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 900000, pi);
                        }
                        else if("30 minutes".equals(items[which]))
                        {
                            PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
                            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1800000, pi);
                        }
                        else if("45 minutes".equals(items[which]))
                        {
                            PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
                            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 2700000, pi);
                        }
                        else if("1 hour".equals(items[which]))
                        {
                            PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
                            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 3600000, pi);
                        }

                    }
                });
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        MyActivity.this.finish();
                    }
                });
                builder.show();

            }
        });

When the user is on the activity, the user can click on Snooze button and click on a time to snooze. When they pick an option, they click ok. I want to be able to close/dismiss the current activity open when they click on ok.

I tried

MyActivity.this.finish(); 

above but it doesn't close the activity.

One thing I noticed is that, it doesn't work the first time and I land back on the same activity however if I go back to the snooze menu again and click on ok, it dismisses the activity. Not sure why this happens.


Solution

  • I took Blaze Tama's advice and in my onClick Method:

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
    
                            Intent menu = new Intent(context, MyOtherActivity.class);
                            context.startActivity(menu);
                            ((Activity) context).finish();
                        }
                    });
    

    It works! :D I hope this helps someone coming around this problem.