Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use setUserVisibleHint in Fragment on Android

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 ?

like image 838
KokoBand Avatar asked May 14 '19 12:05

KokoBand


3 Answers

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
    }
like image 63
Mayur Avatar answered Nov 12 '22 11:11

Mayur


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

like image 24
Aleyam Avatar answered Nov 12 '22 10:11

Aleyam


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

  override fun setMenuVisibility(menuVisible: Boolean) {
        super.setMenuVisibility(menuVisible)
  }
like image 1
jai khambhayta Avatar answered Nov 12 '22 11:11

jai khambhayta