Send data (ArrayList) from Activity to Fragment

I have only one Activitywith one associate Fragment. Fragmentis part of Navigation Drawer. Now I have ArrayList<String>one that continues to change. I want so that whenever I open Navigation Drawer, ArrayListshould be referred to Fragment.

Fragmenthas ListViewwhich is filled in with ArrayList.

This is Activitywith the code for Toolbarand 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 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 ran into the following problem: how to transfer ArrayList<String>from Activityto Fragment.

Also, I cannot see at which point in the code ActivityI can actually transfer data.

PS: - , YouTube. .

+4
2

.

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

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

EventBus ( https://github.com/greenrobot/EventBus). , NavigationDataChangedEvent, NavigationDataChangedEvent Fragment.onViewCreated() unregister (, ) Fragment.onDestroyView(). NavigationDataChangedEvent , , . EventBus, ...

+1
source

All Articles