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