3

I am using a ViewPager in conjunction with ActionBar tabs, as illustrated here. I'm using ActionBarSherlock for backward compatibility, so the parent activity extends SherlockFragmentActivity, and the children fragments extend SherlockFragment.

The solution works great for tabs with swiping, but now I want to dynamically change a fragment associated with one of the tabs.

I've read through numerous S.O. answers on this subject (for example here and here), but I've not found a clear explanation of how to dynamically change a fragment when using ViewPager + the TabsAdapter above.

Here's what I have now. When the user hits a button on an existing fragment, I try to replace the fragment from the parent activity as follows:

AnotherFragment fragment = new AnotherFragment();           
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

int viewId = R.id.pager;                 // resource id of the ViewPager
int position = Constants.TAB_2;          // tab pos of the fragment to be changed

String tag = "android:switcher:" + viewId + ":" + position;

ft.replace(viewId, fragment, tag);
ft.commit();

mTabsAdapter.notifyDataSetChanged();

This doesn't work, so I'm missing something. I also tried doing it with nested fragments using getChildFragmentManager(), but ran into a problem since this function isn't available without API 17, Android 4.2.

Thanks for any help!

Community
  • 1
  • 1
gcl1
  • 3,970
  • 11
  • 36
  • 55
  • It looks like one mistake was using the resource id of the ViewPager, R.id.pager. I switched this to the id of the target fragment layout, and now the new fragment is visible. However, it leaves the old fragment still visible on the tab. Is this heading the right direction, or do I need to be trying something else? – gcl1 Feb 11 '13 at 19:28
  • 1
    UPDATE: I upgraded to API 17 (Android 4.2) so I could use getChildFragmentManager(), as recommended by @georgiecasey in answer to http://stackoverflow.com/questions/7723964/replace-fragment-inside-a-viewpager/13925130#13925130. However, there is still a loose end that causes the old and new fragments to be visible on the tab at the same time. I would most appreciate any reference code that shows how to use nested fragments with the Google TabsAdapter in http://developer.android.com/reference/android/support/v4/view/ViewPager.html. Thanks. – gcl1 Feb 12 '13 at 16:37
  • can you post adapter code as well? – vipul mittal Jan 30 '14 at 11:23

1 Answers1

1

I've made a little example that shows a similar behaviour. I hope you can reuse it.

I think the key is to use a fragment as a container and replace your "real" fragment using it.

For example, see how to navigate to another fragment:

        btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentTransaction trans = getFragmentManager()
                    .beginTransaction();
            /*
             * IMPORTANT: We use the "root frame" defined in
             * "root_fragment.xml" as the reference to replace fragment
             */
            trans.replace(R.id.root_frame, new SecondFragment());

            /*
             * IMPORTANT: The following lines allow us to add the fragment
             * to the stack and return to it later, by pressing back
             */
            trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            trans.addToBackStack(null);

            trans.commit();
        }
    });

You can check the entire example here:

https://github.com/danilao/fragments-viewpager-example

dlao
  • 251
  • 2
  • 9
  • 1
    Note that [link-only answers are discouraged](http://meta.stackoverflow.com/tags/link-only-answers/info), SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference. – kleopatra Jan 30 '14 at 10:15