How to use setUserVisibleHint in Fragment on Android
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)
}
KokoBand
Updated on July 06, 2022Comments
-
KokoBand almost 2 years
In my application i have
BottomNavBar
and i want showfragments
when click on items of thisBottomNavBar
!
For set thisfragments
withBottomNavBar
i usedNavigationGraph
component!I want use
setUserVisibleHint
method for one of thisfragment
, but when show thisfragment
not callsetUserVisibleHint
!My Activity codes for set
fragments
toBottomNavBar
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
intofragment
? -
KokoBand about 5 yearsthanks my friend, it's work to me. but why deprecated this
setUserVisibleHint
? -
Mayur about 5 yearsNO...it's not deprecated. its still working.. welcome... happy coding...
-
EpicPandaForce about 5 yearsIt will be deprecated quite soon, actually. The next AndroidX Fragment release deprecates
setUserVisibleHint
. -
Yasiru Nayanajith almost 5 yearsNow its deprecated
-
fattire almost 5 yearsWhy 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 over 4 yearshow if applying in ViewPager2?
-
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 almost 4 yearsPlease don't simply post code. Add some context to your answer.
-
Aman Verma about 3 yearsWhat about normal onResume when i came back from background?