有很多(重复)的问题和答案,我经历了几乎所有问题并失败了。关于this问题,我最近做了一些更改。

简介:在我的应用程序中,MainActivity拥有Fragment View Pager和FrangmentA,B和C。FrangmentA在单击上显示一个DialogFragment CDialog。关闭CDialog之后,我需要调用FragmentA的doReload(),这在这里没有发生。

主要 Activity

protected void onCreate(Bundle savedInstanceState){
                          ...

            mSectionsPageAdapter = new FragmentAdapter(getSupportFragmentManager());
            mViewPager = (ViewPager) findViewById(R.id.container);
            setupViewPager(mViewPager);
            TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(mViewPager);
            int defaultValue = 0;
            int page = getIntent().getIntExtra("One", defaultValue);
            mViewPager.setCurrentItem(page);
    }

    private void setupViewPager(ViewPager viewPager)
        {
            FragmentAdapter adapter = new
            FragmentAdapter(getSupportFragmentManager());
            adapter.addFragment(new FragmentA(), "FragA");
            adapter.addFragment(new FragmentB(), "FragB");
            adapter.addFragment(new FragmentC(), "FragC");
            viewPager.setAdapter(adapter);
        }

片段A
    public class FragmentA extends Fragment implements CDialog.Dismissed{
    @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    ...
    button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                   FragmentManager fm =  getActivity().getFragmentManager();
                    DialogFragment f = new CDialog();
                    f.show(fm, "CDialog");
                    }
            });

 @Override
    public void dialogDismissed() {
        Log.e(DFD_1, "dialogDismiss Called" );// <-- This is not working*
        doReload();
    }
    }

和CDialogue
public  class CDialog extends DialogFragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
                                    ....
                      return v;
   }
  @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
           ...

            dfd_1.setOnClickListener(
            new View.OnClickListener() {
                public void onClick(View v) {
                        getDialog().dismiss(); //<--when this happens*
                    }
            });

}
    @Override
        public void onDismiss(DialogInterface dialog) {
            if (getActivity() != null && getActivity() instanceof Dismissed) {
                ((Dismissed) getActivity()).dialogDismissed();
            }
            super.onDismiss(dialog);
        }

        public interface Dismissed {
            public void dialogDismissed();  //<-- FragmentA implements this
        }
}

最佳答案

您总是可以直接回调到Fragment本身。

第一步是使用setTargetFragment()设置targetFragment:

DialogFragment#setTargetFragment(Fragment fragment, int requestCode);

我这样做:
public void showDialogFragment(Fragment targetFragment, AppCompatDialogFragment appCompatDialogFragment, int requestCode) {
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
    appCompatDialogFragment.setTargetFragment(targetFragment, requestCode);
    fragmentTransaction.add(appCompatDialogFragment, appCompatDialogFragment.getClass().getSimpleName());
    fragmentTransaction.commitAllowingStateLoss();
}

然后调用此方法以打开对话框片段:
public static final int RC_CDIALOG = 111;

button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
               showDialogFragment(FragmentA.this, new CDialog(), RC_CDIALOG);
            }
        });

然后,在DialogFragment的onDismissListener()中,输入如下代码:
@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);

    if (getTargetFragment() instanceof FragmentA)
        ((FragmentA) getTargetFragment()).doReload();

}

您这样做的方式是:

显示对话框片段“CDialog”,并告诉您目标片段为“FragmentA”,如果与它有关,可以使用其引用。在您的情况下,您必须致电doReload();

09-27 05:16