Question

I have a perfectly fine running FragmentActivity with a smooth horizontal sliding between the (identical) Fragments. I was wondering now how I can send a variable to the Fragment objects?

My code looks like this:

    private void initialisePaging() {

    fragments = new Vector<Fragment>();

    for (int i = 0; i < wiList.length; i++) {
        WhiteboardImage tmpWi = wiList[i];

        intent.putExtra("displayNow", tmpWi.getFilename());
        fragments.add(Fragment.instantiate(this, GalleryFragment.class.getName()));
    }

    this.mPagerAdapter = new GalleryPagerAdapter(super.getSupportFragmentManager(), fragments);

    ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
    pager.setAdapter(this.mPagerAdapter);

    // Define which item of the list will be displayed
    pager.setCurrentItem(4);
}

So basically in the for() loop I am adding multiple times the GalleryFragment object to the PagerAdapter. What I now want to do is that each GalleryFragment gets a different ID (or string for that matter). I could not figure out how to pass a different value to each GalleryFragment object within that loop. At the moment each object gets the very same value.

Thanks for any help.

Was it helpful?

Solution

I usually made it like this: If the data are available from beginning and not change then I parsed them as parameters of fragment.instantiate. then in this function you can put them as a Bundle with .putArguments(...) to your fragment. And in your fragment's onCreate you can retrieve the data with .getArguments(...)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top