Kevin Richards
Kevin Richards

Reputation: 570

Send data (ArrayList) from Activity to Fragment

I have only one Activity with only one associated Fragment. The Fragment is part of the Navigation Drawer. Now I have an ArrayList<String> which keeps changing. I want it such that, whenever I open the Navigation Drawer, the ArrayList should be passed to the Fragment.

The Fragment has a ListView which is populated with the ArrayList.

This is the Activity with the code for Toolbar and the DrawerLayout:

toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);

OnlineNavDrawerFragment drawerFragment = (OnlineNavDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.fragmentUsers);
drawerFragment.setUp((DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

This is the corresponding code of the Fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_online_nav_drawer, container, false);
    ListView lvOnline = (ListView) view.findViewById(R.id.lvOnlineUsers);
    return view;
}

public void setUp(DrawerLayout drawerLayout, Toolbar toolbar) {
    mDrawerLayout = drawerLayout;
    mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout,
            toolbar, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getActivity().invalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            getActivity().invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
    });
}

I am facing the following problem: how to pass ArrayList<String> from the Activity to the Fragment.

Also, I can't see at which point in the Activity code I can actually pass the data.

PS: Most of the code is modified version from one shown in a YouTube video series. And it works in its present form.

Upvotes: 4

Views: 2573

Answers (2)

sockeqwe
sockeqwe

Reputation: 15929

Use an EventBus ( like https://github.com/greenrobot/EventBus ). Define your own event like NavigationDataChangedEvent and register your Fragment for NavigationDataChangedEvent in Fragment.onViewCreated() and unregister (important to avoid memory leaks) in Fragment.onDestroyView(). The Activity can simply post a NavigationDataChangedEvent whenever data has changed to inform the fragment. By using an EventBus your activity and fragment are decoupled ...

Upvotes: 1

doubleA
doubleA

Reputation: 2456

In your fragment do this.

GetDataInterface sGetDataInterface;


public interface GetDataInterface {
    ArrayList<String> getDataList();
}

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

    @Override
public void onResume() {
    super.onResume();
    if(sGetDataInterface != null){
            mData = sGetDataInterface.getDataList();
    }
}
}

In your main activity

public class MainActivity implements YourFragmentName.GetDataInterface {
    @Override
    public ArrayList<String> getDataList() {
        return mDataArrayList;
    }
}

Upvotes: 3

Related Questions