工具列表

  1. setBundle :为 fragment 设置 arguments,返回 Fragment
  2. setArguments :为 fragment 设置 arguments,返回对应类
  3. bundleK :fragment 获取 arguments 的值

使用示例

  1. class TestFg : Fragment() {
  2. //初始化 fragment 并传值
  3. companion object {
  4. private const val key = "key"
  5. fun newInstance(): TestFg = TestFg().setArguments(key to "hello")
  6. fun newInstance2(): Fragment = TestFg().setBundle(key to "value")
  7. }
  8. //获取传值
  9. val value by bundleK<String>(key)
  10. //获取传值,带默认值
  11. val value2 by bundleK<String>(key){
  12. "默认值"
  13. }
  14. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  15. super.onViewCreated(view, savedInstanceState)
  16. }
  17. }