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();
}
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();
}