9

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.

Saad Farooq
  • 13,172
  • 10
  • 68
  • 94

7 Answers7

21

This wasn't possible but there is a workaround for this using aToolbar. Now you can include Toolbar as part of your DialogFragment layout xml and can set its design/icon according to your needs. You will also need to implement setNavigationOnClickListener if you want the back button to behave like it does normally. See the sample class below.

package com.package.name;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;



public class MyDialogFragment extends DialogFragment {
    private View parentView;
    private Toolbar toolbar;


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.Theme_AppCompat_NoActionBar);
        return super.onCreateDialog(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //The layout xml file contains the toolbar
        parentView = inflater.inflate(R.layout.dialogfragment_createpost, container, false);
        initView();
        initData();
        return parentView;
    }


    private void initView() {
        toolbar = (Toolbar) parentView.findViewById(R.id.toolbar);

    }

    private void initData() {
        toolbar.setTitle("Post");
        //Set naviagtion icon to back button drawable
        toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// handle back button naviagtion
                dismiss();
            }
        });
    }
}
Sheraz Ahmad Khilji
  • 8,300
  • 9
  • 52
  • 84
14

There is no way to attach an ActionBar to the DialogFragment even though you can set the theme of the DialogFragment it will not register as a ActionBar 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

Community
  • 1
  • 1
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • 2
    Unfortunately this seems to be the right answer currently – Saad Farooq Jun 19 '14 at 20:46
  • There is a way, to attach toolbar instead, which works about the same as actionbar. Here: http://stackoverflow.com/a/38917527/878126 – android developer Aug 12 '16 at 12:24
  • "the most recent features are added to the support library's version of Toolbar, and they are available on any device that can use the support library. For this reason, you should use the support library's Toolbar class to implement your activities' app bars." https://developer.android.com/training/appbar/setting-up – samus Mar 14 '19 at 17:24
2

In order for the DialogFragment to receive calls to onOptionsItemSelected(MenuItem item) you need the set setHasOptionsMenu(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:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // Respond to the action bar's Up/Home button
        onBackPressed();
        return true;
    }
}
Larry McKenzie
  • 3,253
  • 25
  • 22
2

Just delegate the up action from the component, that receives it (i.e. the parent Activity or Fragment), to your DialigFragment. When up occurs, check if your DialogFragment is shown and if so, call the apropriate method.

einschnaehkeee
  • 1,858
  • 2
  • 17
  • 20
2

Check out here for detailed description.

I solved these problem by adding the below coding.

@Override
public boolean onOptionsItemSelected(MenuItem item) { 
        switch (item.getItemId()) {
        case android.R.id.home: 
            onBackPressed();
            return true;
        }

    return super.onOptionsItemSelected(item);
}

In MainActivity:

Previously,

public class MainActivity extends BaseActivity

Then I change into

public class MainActivity extends FragmentActivity

Most probably you need to extend an MainActivity to FragmentActivity.

Community
  • 1
  • 1
Stephen
  • 9,899
  • 16
  • 90
  • 137
  • Wait ?? You get that callback in the `DialogFragment` ?? – Saad Farooq Jun 14 '14 at 23:35
  • @SaadFarooq yes In DialogFragment you have to use intent to pass it to that activity for Action bar navigation button. – Stephen Jun 16 '14 at 05:25
  • No go... And where is the intent? – Saad Farooq Jun 16 '14 at 21:36
  • @SaadFarooq firstly you need to go through these link.Read the questions and answers posted by me. `http://stackoverflow.com/questions/24032956/action-bar-back-button-not-working`.Then check out the answer.In `GalleryFragment` you see the `Intent`.For Eg: you have to use `intent` in `Fragment` Code then pass it to `Activity` code.In `Activity` Code you have to set the `homeAsUp action button`.Then in `Manifest` you have to set these `Activity Name`.That`s it. – Stephen Jun 17 '14 at 05:06
  • @SaadFarooq Simply use the intent like these. `Intent intent = new Intent(getActivity(), YourActivity.class); startActivity(intent);` in `onClick` or `onItemClick` method in Fragment code. – Stephen Jun 17 '14 at 05:09
1

The following code worked for me:

@Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

Hope this helps :)

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
1

define toolbar in layout and call it on your dialog fragment

Toolbar toolbar=dialog.findViewById(R.id.toolbar);
                           toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
                          toolbar.setOnClickListener(new View.OnClickListener() {
                              @Override
                              public void onClick(View view) {
                                  dialog.dismiss();
                              }
                          });
roshan posakya
  • 1,010
  • 10
  • 14