Up ActionBar action on DialogFragment

谁说我不能喝 提交于 2019-12-09 16:32:55

问题


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.


回答1:


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




回答2:


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();
            }
        });
    }
}



回答3:


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.




回答4:


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.




回答5:


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;
    }
}



回答6:


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




回答7:


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();
                              }
                          });


来源:https://stackoverflow.com/questions/24134714/up-actionbar-action-on-dialogfragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!