Search code examples
androidandroid-fragmentsandroid-bundle

Dialog fragment bundle not passing data


I am opening a dialogfragment and bundling data. I am not sure why but the bundled data does not seem to be past. I have looked for a solution on here but I can't see what I am doing wrong:

This is how I open the fragment and attach the bundled data:

FragmentManager fm = ((MainActivity)context).getSupportFragmentManager();
InfoFragment infoFragment = new InfoFragment();
Bundle bundle = new Bundle();
bundle.putString("TITLE", reminder.getReminderTitle());
bundle.putString("DESCRIPTION", reminder.getReminderDescription());
infoFragment.setArguments(bundle);

// Show DialogFragment
infoFragment.show(fm, "InfoFragment");

And in the fragment I am retrieving the bundled data like this:\

public class InfoFragment extends DialogFragment implements View.OnClickListener{

   TextView titleTextView;
   TextView descriptionTextView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_info, container,false);

        // Iniate ui element
        titleTextView = view.findViewById(R.id.infoFragmentTitleTextView);
        descriptionTextView = view.findViewById(R.id.infoFragmentDescriptionTextView);

        // Get the bundled reminder info to display
        String title = getArguments().getString("TITLE");
        String description = getArguments().getString("DESCRIPTION");

        //Set the values for the textviews from the bundled data
        titleTextView.setText(title);
        descriptionTextView.setText(description);

        return view;

Solution

  • I have written the below code in my activity and kept the same code in dialog fragment as yours and its working perfectly fine

         FragmentManager fm = getSupportFragmentManager();
        InfoFragment infoFragment = new InfoFragment();
        Bundle bundle = new Bundle();
        bundle.putString("TITLE", "Your title");
        bundle.putString("DESCRIPTION", "Your description");
        infoFragment.setArguments(bundle);
    
        // Show DialogFragment
        infoFragment.show(fm, "InfoFragment");
    

    can you tell me where you have written your code to open your fragment