0

I have implemented yes / no dialogue that is shown when back button is pressed. if the user clicks "yes I want to leave" the app closes. everything works fine in my emulator, however when I installed the app on my phone, while clicking "yes" the app goes one step/action backward and closes only when it goes through all the changes that I have made in it. It happens only with my galaxy s4, and what's the most interesting, I did not have the same issue when I installed the app a month ago on the same phone. Anyway, here is the code for yes no dialogue:

public class YesNoDialog extends DialogFragment {

    Button btnYes, btnNo;
    static String dialogTitle;

    public interface YesNoDialogListener {
        void onFinishYesNoDialog(boolean state);
    }

    public YesNoDialog(){

    }


    public void setDialogTitle(String title){
        dialogTitle = title;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_yes_no_dialog, container, false);

        btnYes = (Button) view.findViewById(R.id.btnYes);
        btnNo = (Button) view.findViewById(R.id.btnNo);

        getDialog().setTitle(dialogTitle);

        btnYes.setOnClickListener(btnListener);
        btnNo.setOnClickListener(btnListener);

        return view;
    }

    private View.OnClickListener btnListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            YesNoDialogListener activity = (YesNoDialogListener) getActivity();

            String state = ((Button) v).getText().toString();
            if (state.equals("Yes")) {
                activity.onFinishYesNoDialog(true);
            } else {
                dismiss();
            }

        }
    };

}

and in MainActivity:

 @Override
public void onFinishYesNoDialog(boolean state) {
    if (state){
        super.finish();
    }
}


private void showYesNoDialog() {
    YesNoDialog yesNo = new YesNoDialog();
    yesNo.setDialogTitle("Leaving Project Planner");
    yesNo.show(mFragmentManager, "yes/no dialog");
}

public void onBackPressed() {
    showYesNoDialog();
}
Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
Ana Koridze
  • 1,532
  • 2
  • 18
  • 28

1 Answers1

2

It should be

state.equalsIgnorecase("Yes")

and call the finish() rather than the super call.

I'd like to suggest a few more changes if you don't mind :)

Don't compare String inside the ViewClick interface you can compare id's instead.

private View.OnClickListener btnListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch v.getId():
            case R.id.btnYes:
                dismiss();
                callback.onFinishYesNoDialog();
            break;
            case R.id.btnNo:
                dismiss();
            break;
        }
}

Rather than having a setDialogTitle() you could create a static method that returns the dialog instance with the dialog's parameters initialized.

Something like this:

private String dialogTitle;
private YesNoDialogListener callback;

public static YesNoDialog newInstance(String title, YesNoDialogListener callback){
    YesNoDialog d = new YesNoDialog();
    d.dialogTitle = title;
    d.callback = callback;
    return d;
}

On your MainActivity initialize the dialog:

YesNoDialog d = YesNoDialog.newInstance("My title...", this);
d.show(mFragmentManager, "yes/no dialog");

Finally your callback on the MainActivity will look like this:

@Override
public void onFinishYesNoDialog() {
     finish();
}
Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
  • I made these changes and now when I press Yes it does not go one step back, however, does not close app either, it starts the activity again as many times as many changes I have made to it. I tried super.onBackPressed instead of finish() but with exactly the same result. – Ana Koridze Aug 23 '15 at 06:16
  • let me know if you have any trouble, and don't forget to accept the answer if it helped you out :) – Rakeeb Rajbhandari Aug 23 '15 at 09:22