Android Fragment: Listen to Show/Hide Callback Event

Aug 13, 2019

If you show/hide fragment (such as using BottomNavigationView), you can listen to Fragment.onHiddenChanged.

class TestFragment : Fragment() {    override fun onHiddenChanged(hidden: Boolean) {        super.onHiddenChanged(hidden)        Timber.d("isVisible=${!hidden}")    }}

onHiddenChanged will not be called the first time the fragment is shown

Called when the hidden state (as returned by isHidden() of the fragment has changed. Fragments start out not hidden; this will be called whenever the fragment changes state from that.

To make sure onHiddenChanged is called during the first time, you can hide and show the fragment when first added.

if (!fragment.isAdded) {    transaction.add(, fragment, tag)    transaction.hide(fragment)}else {}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.