Java – Send data (ArrayList) from an activity to a fragment

Send data (ArrayList) from an activity to a fragment… here is a solution to the problem.

Send data (ArrayList) from an activity to a fragment

I only have one activity and only one related fragment. Fragment is part of Navigation Lader. Now I have an ArrayList<String> constantly changing. I want to do so that whenever I open the Navigation Lader, the ArrayList should be passed to the Fragment

Fragment has a ListView populated with an ArrayList

This is the Activity code for Toolbar and 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 for 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’m faced with the following problem: how to < String > via ArrayList from Activity to Fragment

Also, I don’t see what bit of code in the activity I can actually pass the data.

PS: Most of the code is a modified version of the code shown in the YouTube video series. It operates in its current form.

Solution

Do this in your fragment.

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;
    }
}

Related Problems and Solutions