pats
pats

Reputation: 1291

Pass object data between fragments and activity in android

I want to pass a complex object between activity and fragments as well as fragments and fragments. Currently, the main activity create a fragment input object and set that as a member of the fragment needed to be open. Similarly, when another fragment wants to load another fragment it creates fragment input and notifies the main activity. See Main and child fragment code below. My question, is this correct implementation. Sometimes I encountered input being null in child activity, if the activity pauses and restarts.

Please tell me what I have done wrong, whats the best way to pass data.

 public class FragmentInput {

    public String url = "";
    public String title = "";
    public String time = "";
     ... other memebers
}

Main Activity

fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
 BaseFragment fragment = new LandingFragment();
 **FragmentInput input = new FragmentInput();
 input.stringinput = stringinput;
 fragment.input = input;
 fragmentTransaction.replace(R.id.fragment, fragment);
 fragmentTransaction.commit();**



    public void replaceFragment(BaseFragment fragment) {

            if (fragment == null)
                return;

            if (fragment instanceof firstFragment) {
                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                fragmentTransaction.setCustomAnimations(0, 0);

                fragmentManager.popBackStackImmediate(null,
                        FragmentManager.POP_BACK_STACK_INCLUSIVE);

                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

            } else {

                String ttag = fragment
                        .getClass().toString();

                Fragment tempF = fragmentManager.findFragmentByTag(ttag);
                if (tempF != null)
                    return;
                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.fragment_enter,
                        R.anim.fragment_exit, R.anim.fragment_leftenter,
                        R.anim.fragment_leftexit);

                fragmentTransaction.replace(R.id.fragment, fragment, ttag);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }


        }

ChildFragment

@Override
    public void onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);
        try {
            activity = getActivity();
            resource = activity.getResources();
            view = getView();

            **if (input != null) {
                String url= input.url;**

       button.onclick(){
            FragmentInput input = new FragmentInput();
            input.url = path;
            input.title = resource.getString(R.string.txt_from_gallery);

            **BaseFragment fr = new otherFragment();
            FragmentChangeListener fc = (FragmentChangeListener)     getActivity();
            fr.setInput(input);
            fc.replaceFragment(fr);**
}
}

Upvotes: 0

Views: 8558

Answers (2)

savepopulation
savepopulation

Reputation: 11921

If your fragments attach to same activity, you can store your objects in activity and access to objects like below:

((YourActivity)getActivity()).getYourObjects();

If you are storing your objects in bundle in you activity i recommand to call the code sample i gave above in onActivityCreated() method of your fragments to avoid null pointer exception.

If you want to pass your objects between activities or fragments in bundle. You should implement Parcelable to your objects and pass them.

What's Parcelable? http://developer.android.com/reference/android/os/Parcelable.html

Parcelable is more efficient but you can check Serializable: http://developer.android.com/reference/java/io/Serializable.html

It's not a good design to pass large objects in bundle.

Also you can pass your objects with interfaces or you can pass them with bus events. You can check Guava or OttoBus. http://square.github.io/otto/

Upvotes: 6

Eliran Kuta
Eliran Kuta

Reputation: 4358

In this case you can use static holder (it's a bit similar to a singleton pattern).

Create a new class

public class Holder
{
    private static FragmentInput input = null;

    public static void setInput(FragmentInput value) { this.input = value; }
    public static FragmentInput getInput() { return input; }
}

In your main activity, after you create your new FragmentInput object hold it on the Holder

Holder.setInput(input);

And you can access it anywhere, simply call

FragmentInput myInput = Holder.getInput();

Upvotes: 2

Related Questions