Search code examples
androidfragmentdialogfragmentonbackpressed

When apply onBackPressed in Fragment which extends dialog Fragment


I want to implement onBackPressed in fragment which extends a DialogFragment. But when overriding it gives an error that "method does not override its superclass".
How to implement it?
See here for a chunk of the code.

See here for backpressed code


Solution

  • @Swati as per your comment, you want to exit your current dialog as well as current activity. The dialog is another level of window that display on top of your current activity. The DialogFragment is just a shell which hold the actual dialog and manage the state such as device rotate.

    The dialog itself has consume the keyevent include backpress as well. So there is no such backpress method on DialogFragment.

    I would recommend you to get the dialog after it was created see here , override this method and let super class create the dialog then set dismiss listener to it. In the dismiss listener callback you can finish your activity and it will return to the previous activity. You can also use getDialog method which should work the same.