2

I am implementing a feed of messages in a list view inside my activity. In this activity, I am showing just the "main thread" of the messages, not the replies to them, sort of social network behavior, when u click on a message, pops up a new window with that message and the replies to it.

After downloading the JSONS from the webserver, we are storating the info in complex objects which we call VO, i.e. UserVO, which storages all of the User's information.

This objects contain Strings, ints, list of other VO's, lists for the messages feeds, etc...

My idea is opening a new FragmentDialog on the top of the screen when one message is clicked for showing the possible replies to this message, and enable the possibility of sending a reply to it.

For this, I would need to pass those VO objects between the Activity and the Fragment in both directions,

  • From the Activity to the DialogFragment: Object with the messages feed.
  • From the Fragment to the Activity: Once the user writes the text, and attaches pictures (URL) I should send an Object back to the Activity

I have been reading quite a lot of information about the fragments and activities, and still I can not find an usable for me solution. I need to pass several objects when initializing the Fragment, which doesn't seem to be possible using the newInstance() method. Plus for using this method I would need to make serializable all of the VO which we are using (complex). Plus I would still need to pass objects (again complex VO) from the fragment back to the activity... is there any way to pass those big objects? Could an interface work for passing information in both ways?

I would really appreciate any help (some tutorial would be awesome), because I couldn't find any solution for this problem, and I am very stacked in here.

Thanks in advance!

user1624956
  • 41
  • 1
  • 4

3 Answers3

3

In your DialogFragmnet class, you create two methods:

1- newInstance -> to make instance of your DialogFragment

2- setter to set your complex object

    public class YourDialogFragment extends DialogFragment {

        ComplexVariable yourVar;

        public static YourDialogFragment newInstance(int arg, ComplexVariable var) {
            YourDialogFragmentfrag = new MoveSongDialogFragment();
            Bundle args = new Bundle();
            args.putInt("count", arg);
            frag.setArguments(args);
            frag.setComplexVariable(var);
            return frag;
        }

        public void setComplexVariable(ComplexVariable var) {
            yourVar = var;
        }
    }

then, to show the dialog

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
    Fragment prev = manager.findFragmentByTag("yourTag");
    if (prev != null) {
        ft.remove(prev);
    }

    // Create and show the dialog.
    DialogFragment newFragment = YourFragmentDialog.newInstance(argument, yourComplexObject);
    newFragment.show(ft, "yourTag");
Karim Aly
  • 559
  • 5
  • 16
2

You can save the Object as a member of your Activity. Make setter & getter for it. You can access the Activity from the Fragment with getActivity() and cast Activity to YourActivity.

That's the general direction to pass information between Fragments and Activities.

Givi
  • 3,283
  • 4
  • 20
  • 23
1

What Givi mentioned is one of the way to do it, but better and efficient way to handle this could be using factory pattern, say in a scenario where you would using Fragment inside multiple activities.

Check how Answer Proper way to give initial data to fragments?

To understand all the approaches, check section http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

  1. Creating event callbacks to the activity
  2. Getters or Setters
  3. Factory Method

And for complex object if passed in Bundle check this Passing data of a non-primitive type between activities in android implementing Paracable should do the trick

Community
  • 1
  • 1
Mayank Mehta
  • 689
  • 1
  • 8
  • 12