0

I have parent fragment which is implemented with tabhost and having two child fragments A and B fragment , And I want to call a method when the child fragment is visible to the user , I have used setUserVisibleHint which is a Override method. but its never called during the child fragement A or B appears to the user.

The following is the code which have used :

@Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.d("setUserVisibleHint***called",""+isVisibleToUser);

        if(isVisibleToUser){
          calltoMethod();

          }



    } 

And Parent fragment code is :

public class ParentFragment extends Fragment{

private TabHost planTabHost;
LinearLayout tabMainLayout;
private static View plansView;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
plansView = inflater.inflate(R.layout.plansfragment, container, false);
planTabHost = (TabHost)plansView. findViewById(android.R.id.tabhost);

    planTabHost.setup();

            planTabHost.addTab(planTabHost.newTabSpec("tab1").setIndicator("A").setContent(R.id.Afragment));
            planTabHost.addTab(planTabHost.newTabSpec("tab2").setIndicator("B").setContent(R.id.Bfragment));



planTabHost.setCurrentTab(1);

}

}

1 Answer 1

0

You can use TabHost.OnTabChangeListener

To know about the current selected tab position.

 planTabHost.setOnTabChangedListener(new OnTabChangeListener() {
                @Override
                public void onTabChanged(String arg0) {         
                    Log.i("Selected Tab", "index::" + planTabHost.getCurrentTab());
                }       
            });  
1
  • setOnTabChangedListner method is called only when user taps on the tab . but I want load B fragment without user interaction.
    – shobhan
    May 11, 2017 at 5:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.