Red
Red

Reputation: 29

How to pass multiple parameters from Single Activity to multiple Fragments

Let’s say I have a

problem Category List Activity

enter image description here

Detail Information Activity implementing the 3 Fragments

enter image description here

I have seen different approaches like using bundle and using interface and I couldn’t understand non-of them. SO PLS I need HELP Guys!!!

Upvotes: 0

Views: 577

Answers (2)

Farrokh Shahriari
Farrokh Shahriari

Reputation: 387

You can pass params by defining new instance in your fragments as below :

public class FragmentA extends BaseFragment {

View view;
PreferencesManager pm;

@BindView(R.id.textview1)
TextView textView1;


public static FragmentA newInstance(String param1,String param2,String param3) {

    Bundle args = new Bundle();
    FragmentA fragment = new FragmentA();
    args.putString("param1", param1);
    args.putString("param2", param2);
    args.putString("param3, param3);
    fragment.setArguments(args);
    return fragment;
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (view == null) {
        view = inflater.inflate(R.layout.fragment_a, container, false);
        ButterKnife.bind(this, view);

        if (getArguments().size() > 0) {
            param1= getArguments().getString("param1");
            param2= getArguments().getString("param2");
            param3= getArguments().getString("param3");

        }
    }
    return view;
}

}

This is how you replace your fragment :

getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_main, FragmentA.newInstance(param1,param2,param3))
                .addToBackStack(null)
                .commit();

Upvotes: 1

Mani Vasagam
Mani Vasagam

Reputation: 820

Try like this

send data from your activity class:

YourFragment fragment = new YourFragment (); fragment.setArguments(data);

Receive data in fragment class:

Bundle bundle=getArguments();

Upvotes: 0

Related Questions