0

How can I receive the OnBackPressed event within a DialogFragment?

I'm creating a custom DialogFragment causing it to call onCreateView() and not onCreateDialog.

Therefore I'm unable to set an OnDismissListener or OnCancelListener since mDialog is null.

I'm calling my DialogFragment like this:

FragmentManager fragmentManager = ((AppCompatActivity) mContext).
                                getSupportFragmentManager();    
DialogFragmentCustom newFragment = DialogFragmentCustom.newInstance(...);
FragmentTransaction transaction = fragmentManager.beginTransaction();
                    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    transaction.add(android.R.id.content, newFragment);
                    transaction.addToBackStack(null);
                    transaction.commit();

Do you have an idea how to solve my problem?

Marcel Gangwisch
  • 8,856
  • 4
  • 23
  • 32
  • can you post the code of activity that you use to show the dialog – Bruno Ferreira Aug 01 '17 at 13:17
  • what do you exactly mean? I'm using the code which I already posted in my question. – Marcel Gangwisch Aug 01 '17 at 13:23
  • how do you show the dialog is pressing a button? I want the code you use to show the dialog. – Bruno Ferreira Aug 01 '17 at 13:24
  • This is actually the code to show the DialogFragment. After the execution of this code - the overriden onCreateView Methode ist called. And showing correctly my DialogFragment. – Marcel Gangwisch Aug 01 '17 at 13:27
  • The other possibilty would be to call: `DialogFragmentCustom newFragment DialogFragmentCustom.newInstance(mIndexPropertiesManager, mFavoriteManager, favorite); newFragment.show(fragmentManager, "Dialog"); fragmentManager.executePendingTransactions();` But then you can not provide a custom layout. – Marcel Gangwisch Aug 01 '17 at 13:31

0 Answers0