Up ActionBar action on DialogFragment

2020-07-08 06:30发布

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.

7条回答
家丑人穷心不美
2楼-- · 2020-07-08 06:58

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

查看更多
狗以群分
3楼-- · 2020-07-08 07:07

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.

查看更多
放我归山
4楼-- · 2020-07-08 07:08

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 :)

查看更多
够拽才男人
5楼-- · 2020-07-08 07:12

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();
            }
        });
    }
}
查看更多
我只想做你的唯一
6楼-- · 2020-07-08 07:14

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;
    }
}
查看更多
走好不送
7楼-- · 2020-07-08 07:18

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();
                              }
                          });
查看更多
登录 后发表回答