5

I want application to show warning message when user press back button and if user select Yes it will go back. And i am in navigation graph fragment.

I have searched many time in stack Overflow and tried:

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle("Save Or Not");
    builder.setMessage("Do you want to save this? ");
    builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            /*Go back:-I dont know how*/
        }
    });
    builder.setNegativeButton("Discard",null);
    builder.show();
}

But it is showing giver error: Method does not override method from its superclass

Edit: I want to set onBackPressedListener(Mobiles back button) in Navigation graphs fragment

3 Answers3

4

When using Fragments 1.1.0 (which is in alpha right now), you can follow the Provide custom back navigation documentation, which shows how to register an OnBackPressedCallback, which allows you to register for onBackPressed() callbacks from within a Fragment:

public class MyFragment extends Fragment {

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // This callback will only be called when MyFragment is at least Started.
    OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {
        @Override
        public void handleOnBackPressed() {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            builder.setTitle("Save Or Not");
            builder.setMessage("Do you want to save this? ");
            builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // Save your content
                save();
                // Then pop
                NavHostFragment.findNavController(MyFragment.this).popBackStack();
            }
            builder.setNegativeButton("Discard",null);
            builder.show();
        });
        }
    });
    requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
  }
}
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • I have version 1.0.0-beta02 fragment version in gradel. Can changing it from gradel to 1.1.0 update its version – Aradhya Gopal Nepal May 27 '19 at 07:33
  • Note the "requireActivity()" prefix above is needed when the OnBackPressedDispatcher is set up from within a Fragment but the prefix is not needed when setting up from within an Activity. – AJW Sep 11 '19 at 01:58
3

Add this line in button clicklistner MainActivity.super.onBackPressed(); Replace your Activity name insted of MainActivity

 builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
          dialog.dismiss();
          MainActivity.super.onBackPressed();
         //Replace your Activity name insted of MainActivity
        }
    });
Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65
1
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                   //Replace Your Own Action Like Adding Toast etc and Also SuperOnbackprssed or any overriden method
                    dialog.cancel();

                }
            });
Usman Ali
  • 425
  • 1
  • 9
  • 31