简体   繁体   中英

Unable to move from activity to fragment

How to move from activity to fragment on button click?

private fun onClickListener() {
            profileImageView.setOnClickListener {
                val i = Intent(this, ProfileFragment::class.java)
                startActivity(i)
            }
}

在这里输入代码

You can't. You can either move from activity to activity or fragment to fragment. In the first case you can use the code you have implemented with

private fun onClickListener() {
        profileImageView.setOnClickListener {
            val i = Intent(this, SecondActivityWhatever::class.java)
            startActivity(i)
        }

}

otherwise you need to implement Navigation Component in order to move from fragment to fragment.

(Another option in your specific case would be using a Custom Fragment Dialog to show the profile)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM