Home > front end >  How to show/hide FloatingActionButton based on the fragment selected in Kotlin?
How to show/hide FloatingActionButton based on the fragment selected in Kotlin?

Time:11-10

I have an activity with a bottom BottomNavigationView, FloatingActionButton and four fragments. I wanted to show the FloatingActionButton only when I am on the 'fragment1' and 'fragment2 and hide it when 'fragment3' or 'fragment4' is selected. On launching the app, the 'fragment1' is shown with the FloatingActionButton and I managed to hide it when I navigate to 'fragment3'. But the issue is when I navigate back to 'fragment1' from 'fragment3' or 'fragment4' the FloatingActionButton is still hidden.

Following is the xml of my 'HomeActivity'

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@ id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@ id/bottom_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@ id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="16dp"
            android:background="@android:color/transparent"
            app:menu="@menu/bottom_nav_menu" />

    </com.google.android.material.bottomappbar.BottomAppBar>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@ id/fab_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_hide_category"
            app:layout_anchor="@id/bottom_appbar"/>

    <fragment
        android:id="@ id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="50dp"
        app:defaultNavHost="true"
        app:navGraph="@navigation/mobile_navigation"
        app:layout_anchor="@id/bottom_appbar"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Following is the code I have in my 'HomeActivity' and I call it from the onCreateView of the fragments to hide it.

fun hideFabOne(){
    binding.fabOne.hide()
}

Then I add the below code in the 'HomeActivity' and call it in from the fragments to show the FAB, the app crashes on launch.

fun showFabOne(){
    binding.fabOne.show()
}

Edit:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.xyz/com.abc.xyz.ui.activities.HomeActivity}: android.view.InflateException: Binary XML file line #112 in com.abc.xyz:layout/activity_home: Binary XML file line #112 in com.abc.xyz:layout/activity_home: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3835)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4011)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8633)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
     Caused by: android.view.InflateException: Binary XML file line #112 in com.abc.xyz:layout/activity_home: Binary XML file line #112 in com.abc.xyz:layout/activity_home: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #112 in com.abc.xyz:layout/activity_home: Error inflating class fragment
     Caused by: kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
        at com.abc.xyz.ui.activities.HomeActivity.showFabOne(HomeActivity.kt:110)
        at com.abc.xyz.ui.fragments.HomeFragment.onCreateView(HomeFragment.kt:102)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
        at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3065)
        at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2988)
        at androidx.fragment.app.FragmentStateManager.ensureInflatedView(FragmentStateManager.java:392)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:281)
        at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:140)
        at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
        at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
        at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
        at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1067)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
        at com.abc.xyz.databinding.ActivityHomeBinding.inflate(ActivityHomeBinding.java:59)
        at com.abc.xyz.databinding.ActivityHomeBinding.inflate(ActivityHomeBinding.java:53)
        at com.abc.xyz.ui.activities.HomeActivity.onCreate(HomeActivity.kt:38)
2021-11-10 11:23:40.557 13300-13300/com.abc.xyz E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:8207)
        at android.app.Activity.performCreate(Activity.java:8191)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3808)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4011)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8633)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

CodePudding user response:

Since you are using app:navGraph make use of addOnDestinationChangedListener to control the visibility of FloatingActionButton.

Inside HomeActivity do this

navController.addOnDestinationChangedListener { _, destination, _ ->
    if(destination.id == /*FRAGMENT ID*/) {
        // hide
    } else {
        // show
    }
}

CodePudding user response:

I don't know your code, I used a demo to try to reproduce the crash and the functionality you wanted. A demo code

I reused the code and added your XML into my XML file.

What's the reason

The debugged code

I added some logs in different methods to check the call sequence to find the crash reason.

The log

My solution: Move the code to the method of onResume in the Fragment

Here is my code MainActivity

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    lateinit var fab: FloatingActionButton

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d("MainActivity", "TANCOLO===> onCreate 000 ")

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navView: BottomNavigationView = binding.navView
        fab = findViewById(R.id.fab_one)
        Log.d("MainActivity", "TANCOLO===> onCreate 111, fab = $fab ")

        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }

HomeFragment

class HomeFragment : Fragment() {

    private lateinit var homeViewModel: HomeViewModel
    private var _binding: FragmentHomeBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.d("HomeFragment", "TANCOLO===> onCreateView() ")
        homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

        val textView: TextView = binding.textHome
        homeViewModel.text.observe(viewLifecycleOwner, Observer {
            textView.text = it
        })
        return root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.d("HomeFragment", "TANCOLO===> onViewCreated  ")

        //get the FAB and show it.
//        (requireActivity() as MainActivity).fab.apply {
//            Log.d("HomeFragment", "TANCOLO===> onViewCreated, fab = $this  ")
//            visibility = View.VISIBLE
//        }
    }

    override fun onResume() {
        super.onResume()
        Log.d("HomeFragment", "TANCOLO===> onResume  ")
        //get the FAB and show it.
        (requireActivity() as MainActivity).fab.apply {
            Log.d("HomeFragment", "TANCOLO===> onResume, fab = $this  ")
            visibility = View.VISIBLE // or View.GONE
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
  • Related