16

I'm using tab fragments in an activity and the actionbar hosts the tabs. What I want to do is that whenever a fragment appears (or re-appears) in the view (selected by the user), I start doing something. I cannot use onResume of the fragment in this case, since all tabs are never really 'paused' when the user selects another tab, so onResume is not called

I can use the two following events from the hosting activity, but I don't want them since I expect the fragment should know this logic on its own and do that task. Any idea? tks.

  @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }
EyeQ Tech
  • 7,198
  • 18
  • 72
  • 126
  • onPuase() and onResume() method called for the Activity.you have to get Fragment by tag. on PagerAdapter class put a tag and findFragmentByTag(tagname) and trigger the event of selected Fragment. – Ravi Kant May 31 '14 at 12:59
  • Try setUserVisibleHint() in the fragment. [See this answer](http://stackoverflow.com/questions/9779397/detect-viewpager-tab-change-inside-fragment) – Doug Simonton May 31 '14 at 13:01

3 Answers3

42

Try setUserVisibleHint() in the fragment as described in this answer. When the fragment is in the selected tab, setUserVisibleHint() will be called with true, and when the fragment is not the selected tab, setUserVisibleHint() will be called with false. This works for me using the support library.

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser)
        Log.d("MyFragment", "Fragment is visible.");
    else
        Log.d("MyFragment", "Fragment is not visible.");
}
Community
  • 1
  • 1
Doug Simonton
  • 1,021
  • 8
  • 14
  • Launch time all fragments are getting this callback with false value. Anyways this callback is not working with app launch time. – kAmol Sep 25 '17 at 08:57
  • This is not a reliable event, its different behaviour based on OS version. Dont use this if you need to support older versions. – Placeable Feb 01 '19 at 09:02
8

You can override setUserVisibleHint(boolean isVisibleToUser) or onHiddenChanged (boolean hidden) method.

  • In case of setUserVisibleHint(boolean isVisibleToUser),
    isVisibleToUser=true when fragment is visible and isVisibleToUser=false when fragment is hidden.

  • In case of onHiddenChanged (boolean hidden), hidden:True if the
    fragment is now hidden, false if it is not visible.

k4ppa
  • 4,122
  • 8
  • 26
  • 36
Farman Ali Khan
  • 822
  • 7
  • 24
-1

I haven't tested this, but I believe you can use the onHiddenChanged method of the Fragment

From the docs:

Called when the hidden state (as returned by isHidden() of the fragment has changed.

http://developer.android.com/reference/android/app/Fragment.html#onHiddenChanged%28boolean%29

user184994
  • 17,791
  • 1
  • 46
  • 52