How to use setUserVisibleHint in Fragment on Android

13,590

Solution 1

Before you put this code you know about fragment life cycle

 override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
           // post your code
        }
    }

    override fun onStart() {
        super.onStart()
        userVisibleHint = true
    }

Solution 2

As setUserVisibleHint(boolean aBoolean) is now deprecated, for those who still want to know when a fragment is visible you can still use

 public FragmentTransaction setMaxLifecycle(Fragment fragment, Lifecycle.State state)

either indirectly with a FragmentPagerAdapter (or FragmentStatePagerAdapter) just by using the new Constructor

MyFPagerAdapter(FragmentManager fm) {
        super(fm ,FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
       //...
}

note that FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT allows the adapter to use setMaxLifecycle internally (through setPrimaryItem) instead of setUserVisibleHint. Hence in your fragment's implementation you won't anymore need to use

setUserVisibleHint(boolean aBoolean)

but instead, onResume() cause now onResume() will be called only for the visible fragment. the rest ones will still be able to reach onCreateView().

The direct way is by using setMaxLifeCycle with FragmentTransaction when adding or switching fragment, where then

setMaxLifecycle(fragment, Lifecycle.State.STARTED);

is equivalent

setUserVisibleHint(false);

and

setMaxLifecycle(fragment, Lifecycle.State.RESUMED);

equivalent to

setUserVisibleHint(true);

then again listen with Fragment's onResume() callback

Solution 3

After AndroidX setUserVisibleHint is deprecated as a replacement we can use the setMenuVisibility Method.

  override fun setMenuVisibility(menuVisible: Boolean) {
        super.setMenuVisibility(menuVisible)
  }
Share:
13,590
KokoBand
Author by

KokoBand

Updated on July 06, 2022

Comments

  • KokoBand
    KokoBand almost 2 years

    In my application i have BottomNavBar and i want show fragments when click on items of this BottomNavBar!
    For set this fragments with BottomNavBar i used NavigationGraph component!

    I want use setUserVisibleHint method for one of this fragment, but when show this fragment not call setUserVisibleHint !

    My Activity codes for set fragments to BottomNavBar items :

    class HomeActivity : BaseActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    
        setupNavigation()
    
        }
    
            private fun setupNavigation() {
            val navController = Navigation.findNavController(this, R.id.homePage_fragmentNavHost)
            NavigationUI.setupWithNavController(homePage_bottomNavBar, navController)
        }
    
        override fun onSupportNavigateUp() = Navigation.findNavController(this, R.id.homePage_fragmentNavHost).navigateUp()
    }
    

    My Fragment codes :

    class HomeDashboardFragment : Fragment(){
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_home_dashboard, container, false)
        }
    
        override fun setUserVisibleHint(isVisibleToUser: Boolean) {
            super.setUserVisibleHint(isVisibleToUser)
            if (isVisibleToUser) {
                Handler().postDelayed({ requireContext().toast("Show") }, 500)
            }
        }
    }
    

    Why not work setUserVisibleHint into fragment ?

  • KokoBand
    KokoBand about 5 years
    thanks my friend, it's work to me. but why deprecated this setUserVisibleHint ?
  • Mayur
    Mayur about 5 years
    NO...it's not deprecated. its still working.. welcome... happy coding...
  • EpicPandaForce
    EpicPandaForce about 5 years
    It will be deprecated quite soon, actually. The next AndroidX Fragment release deprecates setUserVisibleHint.
  • Yasiru Nayanajith
    Yasiru Nayanajith almost 5 years
    Now its deprecated
  • fattire
    fattire almost 5 years
    Why is this deprecated?! I've read the description of setMaxLifeCycle twice and still have no idea how it would replace setUserVisibleHint-- which I use to hide the soft keyboard and take other actions (trigger animations for example) in a ViewPager when a fragment comes fully into view.
  • Latief Anwar
    Latief Anwar over 4 years
    how if applying in ViewPager2?
  • DIRTY DAVE
    DIRTY DAVE over 4 years
    but instead, onResume() cause now onResume() will be called only for the visible fragment solved my problem thanks
  • Martin Gal
    Martin Gal almost 4 years
    Please don't simply post code. Add some context to your answer.
  • Aman Verma
    Aman Verma about 3 years
    What about normal onResume when i came back from background?