Java – listen for dialogfragment events from viewpager fragment

There are many (repeated) questions and answers available. I have experienced almost all the questions and failed. When referring to this question, I have made some changes recently

Introduction: in my application, mainactivity holds fragment view pager and fragment A, B and C. fragment a displays dialogfragment CDialog on the client. After dismissing CDialog, I need to call fragment A's doreload(), which will not happen here

Main activities

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

FragmentA

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

resolvent:

You can call back the fragment itself directly at any time

First, use settargetfragment() to set targetfragment:

DialogFragment#setTargetFragment(Fragment fragment, int requestCode);

I did this:

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

Then invoke this method to open the dialog box fragment:

public static final int RC_CDIALOG = 111;

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

Then, in ondismisslistener() of dialogfragment, there are some codes as follows:

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);

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

}

What you do is:

Display the dialog fragment "CDialog" and tell it that your target fragment is "fragmenta". You can use its reference because you have something related to it. In your case, you must call doreload();

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>