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.
ActivityandFragment.