8

I have a fragment AroundMeFragment which has a view pager with two fragments MapFragment and ListFragment. When I enter the first time in this fragment all is ok, but when I click an item to enter to a detail view in a new fragment, and return back to AroundMeFragment, the view is blank and no item is draw. And, if I exit from this fragment and enter another time the fragment is blank.

How I can see the content of the fragments when I return back to AroundMeFragment from a detail fragment?

ArouundMeFragment:

public ArroundMeFragment() {
}

public static Fragment newInstance(Bundle bundle) {
    ArroundMeFragment f = new ArroundMeFragment();
    f.setArguments(bundle);
    return f;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.activity = activity;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    sectionShowed = MAP_VIEW_ARROUND_ME;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.arround_me_main_layout, container, false);

    mViewPager = (ViewPager) v.findViewById(R.id.viewPager);

    if (fragments == null) {
        fragments = new ArrayList<Fragment>();

        fragments.add(MapMainFragment.newInstance());

        Bundle bundle = new Bundle();
        bundle.putInt(ConstantsTypeSection.TYPE_VIEW_SECTION, ConstantsTypeSection.AROUND_ME_SECTION);
        fragments.add(DetailShapelessListViewFragment.newInstance(bundle));
    }

    mAdapter = new AroundMePagerAdapter(getFragmentManager(), fragments);
    mViewPager.setAdapter(mAdapter);



    // BTN CHANGE VIEW
    btn_change_view = (ImageButton) v.findViewById(R.id.btn_change_view);
    btn_change_view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            changeView();
        }
    });

    return v;
}

/**
 * Cambia una vista u otra
 */
private void changeView() {

    try {

        if (sectionShowed == LIST_VIEW_ARROUND_ME) {

            mViewPager.setCurrentItem(0);

            sectionShowed = MAP_VIEW_ARROUND_ME;

            btn_change_view.setImageResource(R.drawable.selector_btn_change_map_arroundme);

        } else if (sectionShowed == MAP_VIEW_ARROUND_ME) {

            mViewPager.setCurrentItem(1);

            sectionShowed = LIST_VIEW_ARROUND_ME;

            btn_change_view.setImageResource(R.drawable.selector_btn_change_list_arroundme);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}// End Method

private class AroundMePagerAdapter extends FragmentPagerAdapter {

    private ArrayList<Fragment> fragments;

    public AroundMePagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}

MainActivity. Only one activity to support all fragments in the app.

 ....
 case ConstantsMenu.ARROUND_ME_SECTION:
     Fragment = ArroundMeFragment.newInstance(bundle);  
     break;
 ....

 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
 ft.replace(R.id.content_frame, mFragment);
 if (hasToAddToBackStack) {
      ft.addToBackStack(null);
 }
 ft.commit();
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
beni
  • 3,019
  • 5
  • 35
  • 55

2 Answers2

13
mAdapter = new MyFragmentPagerAdapter(getChildFragmentManager());

Instead of

mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());

as by Austyn Mahoney

Community
  • 1
  • 1
Goodlife
  • 3,822
  • 2
  • 24
  • 23
6

Nested fragment aren't compatible in version 16 and lower. Now, you can nest fragment dynamically.

I change my code and now I'm not using viewpager and add the fragment like this code to switch a fragment to another.

getChildFragmentManager().beginTransaction().replace(R.id.container, list, "LIST").setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
laalto
  • 150,114
  • 66
  • 286
  • 303
beni
  • 3,019
  • 5
  • 35
  • 55
  • 3
    *Nested fragment aren't compatible in version 16 and lower.* - the native ones. You can however use the fragments from the compatibility package which are basically the same. Your question's code has one problem, if you plan to use nested fragments then use `getChildFragmentManager()` instead of `getFragmentManager()`. – user Aug 07 '13 at 08:55
  • You can achieve this without the getChildFragmentManager() by using the adapter I provided here : http://stackoverflow.com/questions/7700226/display-fragment-viewpager-within-a-fragment/18234088#18234088 – Slickelito Aug 14 '13 at 14:13