1

There is an Activity which has fragments and their child fragments. A dynamically produced string array needs to be exchanged in between two of child fragments. But, whatever I've used techniques for that, it returned null everytime.

I also used custom interface implementations even though they are painful in my design. If you can suggest any solution for this problem, I will be appreciated.

enter image description here

Fragment-X and Fragment-Y are defined in a ViewPager Adapter and onPauseFragment() and onResumeFragment() methods are implemented by a custom OnPageChangeListener() interface method.

public class Fragment_X extends SherlockListFragment implements FragmentLifecycle
{
  ...
    @Override
    public void onPauseFragment() 
    {
      String[] outpuStringArray = {"text1","tex2"};
      Fragment_X fragment = new Fragment_X();
      Bundle bundle = new Bundle();
      bundle.putStringArray("selectedItems", outputStringArray);
      fragment.setArguments(bundle);
      Log.d("posted bundle", String.valueOf(bundle));

      bus = new Bus();
      bus.post(bundle);
    }
   ...
 }

Fragment_Y is supposed to take the bundle message when ViewPager item position changed

public class Fragment_Y extends SherlockListFragment implements FragmentLifecycle
{
 ...
    private static String[] list_subject_name;
 ...
    @Override
    public void onResumeFragment() 
    {
      Bundle bundle = getArguments();
      Log.d("received bundle", String.valueOf(bundle));
      if(getArguments()!=null)
      {
        list_subject_name = bundle.getStringArray("selectedItems");
      } else {
            list_subject_name = new String[] {""};
      } 
      bus = new Bus();
      bus.register(this);    
    }
    ...
}

The FragmentLifecycle interface class to control state change between fragments

public interface FragmentLifecycle {

public void onPauseFragment();
public void onResumeFragment();
...
}

EDIT: ViewPager adapter

public class ViewPagerAdapter extends FragmentPagerAdapter
{
public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

// Declare the number of ViewPager pages
final int PAGE_COUNT = 3;
private String titles[] = new String[] { "Fragment_X", "Fragment_Y", "Fragment_Z" };

@Override
public Fragment getItem(int position)
{
    switch (position)
    {
    case 0:
        Fragment_X tab1 = new Fragment_X();
        return tab1;

    case 1:
        Fragment_Y tab2 = new Fragment_Y();
        return tab2;

    case 2:
        Fragment_Z tab3 = new Fragment_Z();
        return tab3;
    }


    return null;
}


public CharSequence getPageTitle(int position)
{
    return titles[position];
}

@Override
public int getCount()
{
    return PAGE_COUNT;
}

}

EDIT: Fragment_B

public class Fragment_B extends SherlockFragment
{
private ViewPagerAdapter pageAdapter; 

public void onAttach(FragmentActivity activity)
{
    super.onAttach(activity);
    setHasOptionsMenu(false);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.layout_fragment_viewpager, container, false);

    pageAdapter = new ViewPagerAdapter(getChildFragmentManager());

    ViewPager mViewPager = (ViewPager) view.findViewById(R.id.registrationViewPager);
    mViewPager.setAdapter(pageAdapter);

    mViewPager.setOnPageChangeListener(pageChangeListener);

    return view;
}

private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

    int currentPosition = 0;

    @Override
    public void onPageSelected(int newPosition) {

        FragmentLifecycle fragmentToShow = (FragmentLifecycle)pageAdapter.getItem(newPosition);
        fragmentToShow.onResumeFragment();

        FragmentLifecycle fragmentToHide = (FragmentLifecycle)pageAdapter.getItem(currentPosition);
        fragmentToHide.onPauseFragment();

        currentPosition = newPosition;
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) { }

    public void onPageScrollStateChanged(int arg0) { }
};


@Override
public void onDetach()
{
    super.onDetach();

    try {
        Field childFragmentManager =
                Fragment.class.getDeclaredField("mChildFragmentManager");

        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
}

EDIT: Here is ViewPger Fragments (Fragment_X and FragmentY) which I try to select list items, collect in a Strin array and publish with a bundle to view in Fragment_Y.

enter image description here

Blo
  • 11,903
  • 5
  • 45
  • 99
cagcak
  • 3,894
  • 2
  • 21
  • 22

1 Answers1

1

You could use an event bus to do that.

The Fragment_Y register to an event, and the Fragment_X publishes that event. Fragment_Y is then notified of the changes.

Exemple of libraries :

Sylphe
  • 1,553
  • 1
  • 12
  • 13
  • I used `bus.post(bundle);` in Fragment_X to publish bundle and `bus.register(this);` in Fragment_Y to reveive published bundle event by your suggession, but absolutely nothing happened. Is that wrong way to publish it? – cagcak May 22 '14 at 14:40
  • Do you use otto? Do the two fragments have to access the same bus instance (with a singleton for exemple)? – Sylphe May 22 '14 at 15:01
  • Yes, and I edited the question by added `ViewPagerAdapter` and `Fragment_B`. I think that I didn't understand `otto` perfectly. Actually, in `VievPager` fragments (`Fragment_X` and `Fragment_Y`) there are two `listview`, and when I execute the adapter, both of them are attached to the activity, it might be cause that I can't receive the bundle after selected list items in `Fragment_X`. – cagcak May 22 '14 at 15:38