Navigate to fragment on FAB click (Navigation Architecture Components)

前端 未结 4 929
囚心锁ツ
囚心锁ツ 2021-01-04 10:40

I have no idea how to, using the new navigation architecture component, navigate from my main screen (with a FloatingActionButton attatched to a BottomApp

4条回答
  •  萌比男神i
    2021-01-04 11:06

    Put it in MainActivity and setOnClickListener in onStart() of the activity and it will work fine.

    override fun onStart() {
      super.onStart()
      floatingActionButton.setOnClickListener {
        it.findNavController().navigate(R.id.yourFragment)
       }
    }
    

    Note:This solution is like and hack and better is to follow Activity LifeCycle and setUp OnClickListener when the activity is ready to interact.

    Similar question [SOLVED]

提交回复
热议问题