user3997016
user3997016

Reputation:

How can i use list<object> of other fragment to another fragment

I have two fragment class:

Mainfragment and subfragment.

I have public List<object> l1 in Mainfragment and i have add items in this list.

Now i want to access this list l1 in subfragment.

i have used - subfragment sf = (subfragment) getActivity().getFragmentManager();

but it shows error.

How can i access this List l1 in subfragment?

Upvotes: 0

Views: 64

Answers (1)

N Kaushik
N Kaushik

Reputation: 2208

It is always better to let the activity apply changes to its fragment than passing values directly between them.

you can achieve this by implementing one interface :

-Implement one interface on MainActivity, say interfaceListener with onQuery(Bundle data) and onResult(Bundle data) methods.

next inside onAttach() of each fragments, implement like this :

public void onAttach(Activity activity) {
    super.onAttach(activity);
    try{
        interfaceListener = (interfaceListener) activity;
     } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement interfaceListener"); 
    } 
 } 

In this way , you can access list of MainFragment in subFragment.

Upvotes: 1

Related Questions