I have a DialogFragment
that is styled to full screen using setStyle(STYLE_NORMAL, R.style.Theme_App)
.
The DialogFragment
shows fine but the up action (the homeAsUp
action on the ActionBar) does not work. I tried implementing onOptionsItemSelected
in the DialogFragment
but it is never called.
Is there a way to get the up action callback in the DialogFragment so I can dismiss it ? For reference, I'm using ActionBarCompat
.
There is no way to attach an
ActionBar
to theDialogFragment
even though you can set the theme of theDialogFragment
it will not register as aActionBar
to it,Dialog.getActionBar()
will always return null.Instead of getting the
ActionBar
you can always attach a Layout that will look like an ActionBar and set the functionality on it using menu.The other way is to create an activity with actionBar as a Dialog you can refer to this post
Check out here for detailed description.
I solved these problem by adding the below coding.
In MainActivity:
Previously,
Then I change into
Most probably you need to extend an
MainActivity to FragmentActivity.
The following code worked for me:
Hope this helps :)
This wasn't possible but there is a workaround for this using a
Toolbar
. Now you can includeToolbar
as part of yourDialogFragment
layout xml
and can set its design/icon according to your needs. You will also need to implementsetNavigationOnClickListener
if you want the back button to behave like it does normally. See the sample class below.In order for the DialogFragment to receive calls to
onOptionsItemSelected(MenuItem item)
you need the setsetHasOptionsMenu(true);
in the onCreate() method of the Fragment.Another potential solution is to handle the up action in the activities
onOptionsItemSelected(MenuItem item)
callback. Something like this:define toolbar in layout and call it on your dialog fragment