Listen DialogFragment rejects the event from the ViewPager fragment

There are many (duplicate) questions and answers; I went through almost all of them and failed. By linking this question, I have made some changes recently.

Short information. In my application, MainActivity supports the Fragment View Pager and FrangmentA, B and C. FrangmentA shows the DialogFragment CDialog onClik dialog box. After rejecting the CDialog, I need to call FragmentA doReload (), which is not happening here.

Mainactivity

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

And 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 
        }
}
+4
source share
1 answer

You always have a direct callback for the fragment itself.

- targetFragment setTargetFragment():

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

+3

Source: https://habr.com/ru/post/1690945/


All Articles