Search code examples
androidtabsactionbarsherlock

How can I replace a fragment used by the tabs in ActionBarSherlock using the Tabs and Pager example?


My question looks similar to this. However, it is a bit more in general: Let me try to explain: I am using the ActionBarSherlock with the Tabsadapter and Viewpager. I have replaced all the fragments with my own and now I have a button in one of my fragments which, when clicked on, starts the following onclick handler:

public void onAcceptSelected() {

    SherlockFragment addFirstChildFragment = AddChildFragment.newInstance();
    getSupportActionBar().setTitle("The new title of the fragment here");
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
     ft.replace(R.id.pager, addFirstChildFragment);
     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
     ft.addToBackStack(null);
     ft.commit();

}

The problem is then that the following method in the new fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
     View v = inflater.inflate(R.layout.fragment_childeditor, container, false);
    return v;
 }

does not result in actually providing a new visible layout: the tab its contents are empty (e.g. I see a holo white themed empty tab).

Now I have the following questions:

My main question is: "How should I replace fragments within a tab?" with subquestions:

  1. Should I adapt the XML of the Tabs and Pager example and fit it with fragment containers?
  2. What is the best practice for using tabs and a viewpager with many fragments? Because I want to use the tabs as my main navigation, but I will have plenty of different fragments.

Solution

  • The answer resides in the answers to this question Actionbarsherlock + tabs + multi fragments?

    Answers to my questions are: 1. No this is not necessary: I can add them programaticllay. 2. I can use fragmentstacks as the example shows in the link above.