如何在 mvp 模式中从 activity 调用片段?

How to call fragments from activity in mvp pattern?

问题是,我无法从 DashboardActivity 移动到 BookingFragment。

//BookingFragment
       companion object {
            fun start(): BookingFragment {
                val fragment = BookingFragment()
                return fragment
    
    
            }



//Calling back this function in activity inorder to move on that fragment
     fun getBookingView() {
            BookingFragment.start()
        }

//Fragment activity
     private lateinit var binding: FragmentBookingBinding
     override fun onCreateView(
                inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
            ): View? {
                super.onCreate(savedInstanceState)
                val mvpKotlinApplication = AppApplication()
                DaggerBookingComponent.builder()
                    .appComponent(mvpKotlinApplication.get(requireActivity()).appComponent)
                    .bookingModule(BookingModule(activity as AppCompatActivity))
                    .build()
                    .inject(this)

//捆绑 绑定 = FragmentBookingBinding.inflate(layoutInflater) val 视图 = binding.root bookingView.start(绑定) bookingPresenter.onCreateView() return 视图 }
 //DashboardPresenter::
         private fun onClick() {
                dashboardView.getBookingObserable().doOnNext { dashboardModel.getBookingView() }
                    .subscribe()
            }

可能您需要在activity中创建片段容器视图并继续进行片段事务。请参考文档 - https://developer.android.com/guide/fragments/create

基本上你会得到这样的东西:

<!-- res/layout/example_activity.xml -->
<androidx.fragment.app.FragmentContainerView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/fragment_container_view"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />

fun getBookingView() {
    supportFragmentManager.commit {
        add(BookingFragment.start())
    }
}