Pass data by pressing tab in android

问题: I wonder how to pass data by pressing tab. I have two tab in activity, named Info and Details. When Details tab is clicked, I want to pass the title editText to next activi...

问题:

I wonder how to pass data by pressing tab. I have two tab in activity, named Info and Details. When Details tab is clicked, I want to pass the title editText to next activity.

enter image description here

P/S : Without button clicked

MainActivity

  tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(p0: TabLayout.Tab?) {
            }

            override fun onTabSelected(p0: TabLayout.Tab?) {
                val position = p0?.position
                when (position) {
                    0 ->
                        supportFragmentManager.beginTransaction().replace(
                            R.id.frame_container,
                            CreateWOInfoFragment()
                        ).addToBackStack(null).commit()
                    else -> supportFragmentManager.beginTransaction().replace(
                        R.id.frame_container,
                        CreateWODetailsFragment()
                    ).addToBackStack(null).commit()
                }
            }

            override fun onTabUnselected(p0: TabLayout.Tab?) {
            }
        })

回答1:

FragmentOne would be sending the data entered in EditText to FragmentTwo. So add an interface in fragment one and let the activity implement this interface, in Fragment one call the method to send data and in main fragment will handle it to send it to fragment two.

Please check this example Android Passing Data between Fragments

  • 发表于 2019-01-20 23:32
  • 阅读 ( 199 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除