How to pass and get value from fragment and activity

前端 未结 9 1267
独厮守ぢ
独厮守ぢ 2020-12-25 11:28

How to pass and get value from fragment and activity?

9条回答
  •  醉酒成梦
    2020-12-25 12:12

    use this to send arguments to fragment

    fun newInstance(index: Int): MyFragment {
        val f = MyFragment ()
        // Pass index input as an argument.
        val args = Bundle()
        args.putInt("index", index)
        f.setArguments(args)
        return f
    }
    

    And get those arguments like this

    val args = arguments
    val index = args.getInt("index", 0)
    

提交回复
热议问题