相关文章推荐
苦恼的山羊  ·  Index of ...·  10 月前    · 
茫然的春卷  ·  在vb ...·  11 月前    · 
  • Kotlin基础入门 - 变量、方法、对象、继承、接口
  • Kotlin实战基础 - 设置点击事件、Activity跳转、传值
  • Kotlin实战基础 - Activity、Fragment传递参数
  • 在我的认知中,关于 Activity → Fragment Fragment → Fragment 传值方式一般有以下几种

  • 传统 setArguments 方式,主要是通过 Bundle 传递
  • 类似 EventBus 三方框架
  • 除传统方式外,主要是采用的是 观察者模式
  • TestFragment testFragment= new TestFragment();
    Bundle args = new Bundle();
    args.putInt("id", 666);
    testFragment.setArguments(args);

    Bundle bundle = getArguments();
    id = bundle.getInt("id");

    伪代码示例

     if (null == testFragment) {
       testFragment= new TestFragment();
     Bundle bundle = new Bundle();
     bundle.putString("name", "拍照");
     testFragment.setArguments(bundle);
     //方法一传参数
     Fragment instantiate = TestFragment.instantiate(this, TestFragment.class.getName(), bundle);
     //方法二传参数
     getSupportFragmentManager().beginTransaction().replace(R.id.ll_replace, instantiate).addToBackStack(this.getClass().getSimpleName()).commit();
    //        getSupportFragmentManager().beginTransaction().replace(R.id.ll_replace, takePhotoFragment).addToBackStack(this.getClass().getSimpleName()).commit();
    

    Kotlin 版本

    只要是熟悉Kotlin语法的朋友,应该不用看,更多的是为了从Java刚转到Kt的朋友,gogogo ~

    Activity 之间传递数据

    Java版本:Activity之间数据传递

    我有点懒,直接copy了这里的伪代码,做个笔记 ~

    基本类型数据
     val bundle = Bundle()
     bundle.putString("String", "text")
     bundle.putInt("Int", 2)
     val intent = Intent(this, BActivity::class.java)
     intent.putExtras(bundle)
     startActivity(intent)
    
     val bundle = intent.extras
     val int = bundle?.getInt("Int")
     val string = bundle?.getString("String")
    

    val bundle = Bundle()
    val array = arrayOf("元素1", "元素2")
    bundle.putStringArray("array", array)
    val intent = Intent(this, BActivity::class.java)
    intent.putExtras(bundle)
    startActivity(intent)

    val bundle = intent.extras
    val array = bundle?.getStringArray("array")

    集合(基本类型)

    val bundle = Bundle()
    val arrayList = arrayListOf("元素1", "元素2")
    bundle.putStringArrayList("arrayList", arrayList)
    val intent = Intent(this, BActivity::class.java)
    intent.putExtras(bundle)
    startActivity(intent)

    val bundle = intent.extras
    val arrayList = bundle?.getStringArrayList("arrayList")

    关于Kt序列化的部分,我之后会补入一篇的!!!

    对象(data class)
    val bundle = Bundle()
    val data = TextData(true, "测试", "数据")
    bundle.putParcelable("textData", data)
    val intent = Intent(this, BActivity::class.java)
    intent.putExtras(bundle)
    startActivity(intent)
    
    val bundle = intent.extras
    val data = bundle?.getParcelable<TextData>("textData")
    Log.e("TAG", "object: $data")
    

    注意这里的data class 需要序列化, 我们可以通过注解@Parcelize简化代码

    import android.os.Parcelable
    import kotlinx.android.parcel.Parcelize
    @Parcelize
    data class TextData(
        val result: Boolean,
        val note: String,
        val `data`: String
    ) : Parcelable
    
    集合(对象)
    val bundle = Bundle()
    val data1 = TextData(true, "成功", "成功数据")
    val data2 = TextData(false, "失败", "失败数据")
    val objectArrayList = arrayListOf(data1, data2)
    bundle.putParcelableArrayList("objectArrayList", objectArrayList)
    val intent = Intent(this, BActivity::class.java)
    intent.putExtras(bundle)
    startActivity(intent)
    
    val bundle = intent.extras
    val objectArrayList = bundle?.getParcelableArrayList<TextData>("objectArrayList")
    Log.e("TAG", "getData: $objectArrayList")
    

    Activity传参到Fragment

    Activity 传值到 Fragment

    val fragment = MvPagerFragment()
    val bundle = Bundle()
    bundle.putString("args", "靓仔")
    fragment.arguments = bundle

    var any = arguments?.get("args")

    instantiate 方式

    instantiate 方式可能已过时,但目前尚可使用

    val bundle = Bundle()
    bundle.putString("type", "1")
    var personalCollectionFragment =
    Fragment.instantiate(this, PersonalCollectionFragment::class.java.name, bundle)

    //以下是我在项目中用到的一段伪代码,可以借鉴
    binding.vp.adapter = object : FragmentStateAdapter(this) {
    override fun createFragment(position: Int) = when (position) {
    0 -> PersonalDynamicFragment()
    1 -> personalCollectionFragment
    else -> PersonalLikeFragment()

            override fun getItemCount() = titleList.size
    

    var any = arguments?.get("type")