简体   繁体   English

在ActionBar.Tab片段中显示DialogFragment

[英]Show DialogFragment in ActionBar.Tab fragments

I have a few Actiobar with 5 tabs each with a fragment. 我有一些带有5个标签的Actiobar,每个标签都有一个片段。 In 3 of this fragments I want to show a Dialog so I've created a new class: 在这个片段的3个中,我想显示一个Dialog,所以我创建了一个新类:

 public static class MyDialogFragment extends DialogFragment {
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        static MyDialogFragment newInstance() {
            return new MyDialogFragment();
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            int style = DialogFragment.STYLE_NORMAL;
            int theme =  android.R.style.Theme_Holo_Dialog;

            setStyle(style, theme);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_dialog, container, false);
            View tv = v.findViewById(R.id.textV);
            ((TextView)tv).setText("Dialog using style Normal - Theme AlertDialog - NoActionBar");


            return v;
        }
    }

In every onCreate method of this 3 fragments I'm trying to show the Dialog by using this method: 在这3个片段的每个onCreate方法中,我试图通过使用此方法来显示Dialog:

private void showPopup()
{
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    Fragment prev = getFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    DialogFragment newFragment = MyDialogFragment.newInstance();
    newFragment.show(ft, "dialog");

}

Now the problem is that this dialog is displayed on tabs that should not. 现在的问题是,该对话框显示在不应显示的选项卡上。

For example I want tabs 1 3 and 5 to display the Dialog - and sometimes it displays it - but sometimes when I tap the tab 2 this dialog appears and if I tap 3 the Dialog is not showed. 例如,我希望选项卡1 3和5显示对话框-有时会显示该对话框-但有时,当我点击选项卡2时,会出现该对话框,如果我点击3,则不会显示该对话框。 What could be the problem and how should I fix it? 可能是什么问题,我应该如何解决? Thanks 谢谢

Have you try to move your showPopup() call in onCreateView() or in onActivityCreated() methods, instead of in onCreate() one ? 您是否尝试过在onCreateView()或onActivityCreated()方法中而不是在onCreate()方法中移动showPopup()调用?

EDIT: According to comments below, the problem is linked to the use of a ViewPager, which prepare some next Fragments to be viewed, and then call onCreate() methods. 编辑:根据下面的评论,该问题与ViewPager的使用有关,该ViewPager准备了要查看的下一个片段,然后调用onCreate()方法。

So I've found a solution - in every fragment I override a method called setMenuVisibility - and test if the the fragment is visible. 因此,我找到了一个解决方案-在每个片段中,我都覆盖了一个名为setMenuVisibility的方法-并测试该片段是否可见。 If it is - I call my method. 如果是-我调用我的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM