Search code examples
androidandroid-fragmentsandroid-tabhost

Switch between tabs in a fragment tabhost


  1. I have a FragmentTabHost, hosted in a Fragment. The tabHost has 4 tabs.
  2. In first tab fragment, there are two nested fragments.
  3. After clicking in a view in first nested fragment, a DialogFragment appears, which has a couple of buttons.

After clicking on a button in DialogFragment, I want to switch to tab number 3. How can I do this?


Solution

  • In Tab Fragment:

    public void setCurrentTab(int tab_index){
        mTabHost = (FragmentTabHost)getActivity().findViewById(android.R.id.tabhost);
        mTabHost.setCurrentTab(tab_index);
        }
    

    In click event of the button of DialogFragment:

    FragmentTabHost tabHost = (FragmentTabHost)  getActivity().findViewById(android.R.id.tabhost);
    tabHost.setCurrentTab(3);