0

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?

1 Answers1

1

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.

N Kaushik
  • 2,198
  • 18
  • 29