Kotlin: open new Activity inside of a Fragment

后端 未结 13 2198
醉话见心
醉话见心 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:18

    Also an option (for kotlin). In onCreateView set onClickListener for a button:

    button.setOnClickListener {
        requireActivity().run {
            startActivity(Intent(this, MainActivity::class.java))
            finish() // If activity no more needed in back stack
        }
    }
    

提交回复
热议问题