Skip to content
Advertisement

Communication Between DialogFragment and Fragment

I am trying to pass information from my alert dialog to my second fragment (I am using tabbed activity layout).

I want to pass information from alert dialog to fragment when I click on my ImageView, but my app keep crashing until I implement my interface inside MainActivity.java. My main mission here is to open alert dialog which contains several buttons. When I click first button I want to print “Test br 3” but it does not work inside my Fragment, it only works inside my MainActivity.java where method prints “Test br 2”.

My Main Activity

public class MainActivity extends AppCompatActivity implements ExercisesAlertDialog.DataTransfer {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);



    }

    @Override
    public void ApplyData() {
       System.out.println("Test br 2");
    }

My Fragment

public class Frag2 extends Fragment implements ExercisesAlertDialog.DataTransfer {


      ImageView plusbtn;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.frag2, container, false);

        plusbtn = (ImageView) v.findViewById(R.id.plusbtn);


        plusbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            ExercisesDialog();
            }
        });



        return v;
    }

public void ExercisesDialog()
{
    ExercisesAlertDialog exercisesAlertDialog = new ExercisesAlertDialog();
    exercisesAlertDialog.show(getFragmentManager(), "Exercises Dialog");
}

    @Override
    public void ApplyData() {
        System.out.println("Test br 3");
    }

My Alert Dialog

public class ExercisesAlertDialog extends AppCompatDialogFragment {

    ImageButton one, two;
    private DataTransfer listener;
    public int first;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View v = inflater.inflate(R.layout.workout_custom_menu, null);
        builder.setView(v);
        builder.setTitle("Choose your Workout");
        builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               dismiss();
            }
        });

        one = (ImageButton) v.findViewById(R.id.first);
        two = (ImageButton) v.findViewById(R.id.second);

one.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        first = 1;
        if(first ==1)
        {
listener.ApplyData();
        }


    }
});


        return builder.create();
    }


    public interface DataTransfer
    {
        void ApplyData();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            listener = (DataTransfer) context;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(toString() + "Must implement DataTransfer");
        }
    }
}

Advertisement

Answer

I changed getChildFragmentManager() instead of getFragmentManager() in the show() call. Second thing is onAttach() to listener = (DataTransfer) getParentFragment(); instead of context.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement