来自intent的解组附加组件会抛出异常但仅在onActivityReenter

时间:2017-08-24 06:26:22

标签: android-intent android-transitions android-bundle

我只是尝试在重新进入转换运行之前实现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时   unmarshalling:letstwinkle.com.twinkle.api.MatchPlaySubmitModel                                            在android.os.Parcel.readParcelableCreator(Parcel.java:2535)                                            在android.os.Parcel.readParcelable(Parcel.java:2461)                                            在android.os.Parcel.readValue(Parcel.java:2364)                                            在android.os.Parcel.readArrayMapInternal(Parcel.java:2717)                                            在android.os.BaseBundle.unparcel(BaseBundle.java:269)                                            在android.os.BaseBundle.getString(BaseBundle.java:992)                                            在android.content.Intent.getStringExtra(Intent.java:6211)                                            at letstwinkle.com.twinkle.MatchPlayActivity.onActivityReenter(MatchPlayActivity.kt:1123)

经过测试的API 24

1 个答案:

答案 0 :(得分:0)

框架转换实施似乎有一个小问题。目前没有“好”的解决方案。可能的解决方法是避免将应用程序定义的类放入结果意图中的任何方法。我选择的方式是该类(幸运的是只有一个),删除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)
}