Kotlin: open new Activity inside of a Fragment

后端 未结 13 2205
醉话见心
醉话见心 2020-12-05 16:32

How can I open a new Activity inside of a fragment when using a button?

I tried this

override fun onViewCreated(view: View, savedInstanceState: Bundle?         


        
13条回答
  •  一生所求
    2020-12-05 17:20

    If you only use activity returns just an Activity instance. This can be any activity that embeds your fragment so in some cases you can get FragmentActivity instead your parent activity. Use this to make use you are getting the correct one:

    (activity as YourParentActivity?)?.let{
        val intent = Intent (it, Main::class.java)
        it.startActivity(intent)
    }
    

提交回复
热议问题