本文介绍了Kotlin:在片段内打开新的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用按钮时在片段内打开一个新的 Activity?

我试过了

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(视图,savedInstanceState)LogOut_btn.setOnClickListener {//FirebaseAuth.getInstance().signOut()val 意图 = 意图(这个,Main::class.java)开始活动(意图)}}

val intent = Intent 似乎在片段中不起作用.

知道如何在片段内启动新活动吗?

解决方案

因为 Fragment 不是 Context 类型,你需要调用父 活动:

 val intent = Intent (getActivity(), Main::class.java)getActivity().startActivity(意图)

或者类似的东西

activity?.let{val 意图 = 意图(它,Main::class.java)it.startActivity(意图)}

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?) {
    super.onViewCreated(view, savedInstanceState)

    LogOut_btn.setOnClickListener {
        //FirebaseAuth.getInstance().signOut()
        val intent = Intent (this, Main::class.java)
        startActivity(intent)
    }
}

Any idea how I can start a new activity inside a fragment?

解决方案

Because Fragment is NOT of Context type, you'll need to call the parent Activity:

 val intent = Intent (getActivity(), Main::class.java)
 getActivity().startActivity(intent)

or maybe something like

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

这篇关于Kotlin:在片段内打开新的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-04 22:01