[Android development] Fragment pass value

Activity passes value to Fragment

method one

//Activity
fun myClick(view: View) {
    
    
        val transaction = supportFragmentManager.beginTransaction()
        when (view.id) {
    
    
            R.id.rb_index -> transaction.replace(R.id.container1, IndexFragment())
            R.id.rb_channel -> {
    
    
                //1. 实例化Fragment
                val f1: Fragment = Fragment1()
                //2. 实例化一个Bundle对象
                val bundle = Bundle()
                //3. 存入数据到Bundle对象
                bundle.putString("msg1", "这是由Activity发往Fragment的数据")
                //4. 调用Fragment的setArguments方法,传入Bundle对象
                f1.arguments = bundle
                //5. 添加/替换显示的Fragment
                transaction.replace(R.id.container1, f1)
            }
            R.id.rb_list -> transaction.replace(R.id.container1, Fragment2())
            R.id.rb_me -> transaction.replace(R.id.container1, Fragment3())
        }
        transaction.commit()
    }
	//Fragment
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
    
    
        // Inflate the layout for this fragment
        val v = inflater.inflate(R.layout.fragment_1, container, false)
        val arguments = arguments
        val msg1 = arguments!!.getString("msg1")
        (v.findViewById<View>(R.id.txt1) as TextView).text = msg1

        return v
    }
class TabFragmentActivity : AppCompatActivity() {
    
    
	fun sendMsg(): String {
    
    
        return "这是一个普通方法传递过去的消息"
    }
}

Method Two

//fragment
	override fun onAttach(context: Context) {
    
    
        super.onAttach(context)
        val msg = (context as TabFragmentActivity).sendMsg()
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
    }

Fragment passes value to Activity

Fragment passes value to Activity (interface callback)

  1. Define an interface and declare a method for passing data in the interface
  2. Let the Activity implement the interface, then rewrite the callback method, get the incoming value, and then do the processing
  3. In the custom Fragment, declare a reference to the callback interface
  4. In the onAttach method, assign a value to the reference in the third step
  5. Method of passing data in reference call
class Fragment3 : Fragment() {
    
    
	//3
    private var myListener: MyListener? = null
    override fun onAttach(context: Context) {
    
    
        super.onAttach(context)
        //4
        myListener = activity as MyListener?
        //5
        myListener!!.sendMsg("消息")
    }

	//1
    interface MyListener {
    
    
        fun sendMsg(msg: String?)
    }
}
//2
class TabFragmentActivity : AppCompatActivity(), Fragment3.MyListener {
    
    
    override fun sendMsg(msg: String?) {
    
    
        Log.e("TAG", "Fragment传回的数据:$msg")
    }
}

Guess you like

Origin blog.csdn.net/weixin_42020386/article/details/112867001