Search code examples
androidandroid-fragmentsandroid-tabhostandroid-lifecyclefragment-tab-host

Are there any " on leave tab " event for the fragment inside the tabhost in android?


I am working on the tabhost and fragment implementation. There are 5 tabs in my tabhost , I would like to implement some code (go back 1 level) whenever the user leave the tab3. However, I tried on detach and on pause , both are not working.

@Override
public void onDetach() {
    // TODO Auto-generated method stub
           getFragmentManager().popBackStackImmediate();
    super.onDetach();
}

So I wonder what is the activity cycle the fragment will at when the user click on the other tab? or , are there any way to capture that event ? Thanks


Solution

  • Try adding this callback to your TabHost:

    myTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener()
    {
    
      @Override
      public void onTabChanged(String tabId)
      {
         // Tab was changed; now do stuff  
      }
    
    });