4

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.

7
  • It depends on your code. You have to show at least the code of your activity. Commented Jul 13, 2015 at 0:21
  • @Foxinsocks Added code for both Activity and Fragment. Commented Jul 13, 2015 at 0:30
  • Instead of passing the data to the fragment when you open the drawer. Have the fragment use an interface to pull the data from the activity when it is needed. Usually in onResume(). Commented Jul 13, 2015 at 0:56
  • I believe if the it will work better if a fragment is instatiated using getInstance() then pass values on it.. Commented Jul 13, 2015 at 0:58
  • @Sheychan if he only sends the data in getInstance then it will not update every time he opens the drawer. Commented Jul 13, 2015 at 1:03

2 Answers 2

3

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Worked like a charm!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.