从意图中解组额外内容会引发异常,但仅在 onActivityReenter 期间

Unmarshalling extras from intent throws an exception but only during onActivityReenter

我刚刚尝试实施 onActivityReenter 以在重新进入过渡运行之前进行特殊的 UI 更新。我从字面上从 onActivityResult 中取出请求代码块并将其分成两部分,一部分进入 onActivityReenter,另一部分留在那里:

BEFORE: (onActivityResult)
            if (resultCode == RESULT_OK) {
                // replace the MatchPlaySubmit model
                data?.let {
                    this.dataBinding.submitModel = data.getMatchPlaySubmitModel()
                    val profIndex = data.getMatchPlayFinalProfileIndex()
                    dataBinding.matchPlay.matchPlayPicker.jumpToProfileNumber(profIndex + 1)
                    this.updatePickerView()
                }
            }


override fun onActivityReenter(resultCode: Int, data: Intent?) {
    if (resultCode == RESULT_OK && data != null) {
        // makes sure that the data is from pager, since there no request code
>        if (data.getStringExtra("source") == "mppager") { // causes CRASH
            val profIndex = data.getMatchPlayFinalProfileIndex()
            dataBinding.matchPlay.matchPlayPicker.jumpToProfileNumber(profIndex + 1)
        }
    }
}

AFTER: (onActivityResult)
            if (resultCode == RESULT_OK) {
                // replace the MatchPlaySubmit model
                data?.let {
                    this.dataBinding.submitModel = data.getMatchPlaySubmitModel()
                    this.updatePickerView()
                }
            }

但是,新代码onActivityReenter总是会导致崩溃。崩溃就像我没有为附加功能正确设置类加载器一样。但是,我刚刚添加到 kotlin 扩展函数中,以确保将额外的类加载器设置为我的。然而,崩溃还是时有发生。

inline internal fun Intent.setMatchPlaySubmitModel(model: MatchPlaySubmitModel?) {
    this.putExtra("@mpsubmit@", model)
    this.setExtrasClassLoader(MatchPlaySubmitModel::class.java.classLoader)
}

android.os.BadParcelableException: ClassNotFoundException when unmarshalling: letstwinkle.com.twinkle.api.MatchPlaySubmitModel at android.os.Parcel.readParcelableCreator(Parcel.java:2535) at android.os.Parcel.readParcelable(Parcel.java:2461) at android.os.Parcel.readValue(Parcel.java:2364) at android.os.Parcel.readArrayMapInternal(Parcel.java:2717) at android.os.BaseBundle.unparcel(BaseBundle.java:269) at android.os.BaseBundle.getString(BaseBundle.java:992) at android.content.Intent.getStringExtra(Intent.java:6211) at letstwinkle.com.twinkle.MatchPlayActivity.onActivityReenter(MatchPlayActivity.kt:1123)

已测试 API 24

看来框架转换实现有问题。目前没有 "good" 解决方案。可能的解决方法是避免将应用定义的 class 放入结果意图中的任何方法。我选择的方法是 class(幸运的是只有一个),删除 Parcelable 实现并从 Intent 和 writeToIntent 方法中提供构造函数:

constructor(intent: Intent) {
    playID = intent.getStringExtra("@mpsubmit@playid")
    chosenProfileID = intent.getStringExtra("@mpsubmit@prof1")
    extraChosenProfileID = intent.getStringExtra("@mpsubmit@prof2")
    passed = intent.getBooleanExtra("@mpsubmit@pass", false)
    shout = intent.getStringExtra("@mpsubmit@shout1")
    extraShout = intent.getStringExtra("@mpsubmit@shout2")
}
fun writeToIntent(intent: Intent) {
    intent.putExtra("@mpsubmit@playid", playID)
    intent.putExtra("@mpsubmit@prof1", chosenProfileID)
    intent.putExtra("@mpsubmit@prof2", extraChosenProfileID)
    intent.putExtra("@mpsubmit@pass", passed)
    intent.putExtra("@mpsubmit@shout1", shout)
    intent.putExtra("@mpsubmit@shout2", extraShout)
}