Fixed
Status Update
Comments
il...@google.com <il...@google.com> #2
Looks to be the same issue as https://github.com/androidannotations/androidannotations/issues/1208 where after being parceled and unparceled, the type swaps to Parcelable[] which is indeed a different type than CustomModel[]. The same thing can be seen with Fragment destinations if you:
1. turn on the Developer Option of "Don't keep activities"
2. Navigate to the Fragment destination (it won't crash)
3. Hit the home button
4. Use the recents button to relaunch the app (it will crash)
It seems like the only viable work around is to create a new array of the correct type (CustomModel[]), then System.arraycopy() the objects over. We can do this automatically as part of the Args class. We should also double check to see if this same problem exists with Serializable.
1. turn on the Developer Option of "Don't keep activities"
2. Navigate to the Fragment destination (it won't crash)
3. Hit the home button
4. Use the recents button to relaunch the app (it will crash)
It seems like the only viable work around is to create a new array of the correct type (CustomModel[]), then System.arraycopy() the objects over. We can do this automatically as part of the Args class. We should also double check to see if this same problem exists with Serializable.
ap...@google.com <ap...@google.com> #3
Project: platform/frameworks/support
Branch: androidx-master-dev
commit 72631a760ebfb23990c42440d0c1f8c67fafe5ed
Author: Daniel Santiago Rivera <danysantiago@google.com>
Date: Thu Feb 07 14:11:06 2019
Copy & Cast Parcelable array items when reading NavArgs from Bundle
Bundle#getParcelableArray() return Parcelable[] which NavArgs was
failing to cast to Item[] where Item is an app defined Parcelable. This
CL changes the generated code so that the Parcelable[] items are copied
into an array of the defined type in the nav graph.
This issue doesn't exist for Serializable since there is no
Bundle#getSerializableArray().
Bug: 123963545
Test: Manual test with sample app provided in issue.
Change-Id: Ibfcb703a1ad48929a0731b82a13a1f33813187b5
M navigation/safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/java/JavaTypes.kt
M navigation/safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/kotlin/KotlinTypes.kt
M navigation/safe-args-generator/src/tests/test-data/expected/java_nav_writer_test/MainFragmentArgs.java
M navigation/safe-args-generator/src/tests/test-data/expected/kotlin_nav_writer_test/MainFragmentArgs.kt
https://android-review.googlesource.com/898454
https://goto.google.com/android-sha1/72631a760ebfb23990c42440d0c1f8c67fafe5ed
Branch: androidx-master-dev
commit 72631a760ebfb23990c42440d0c1f8c67fafe5ed
Author: Daniel Santiago Rivera <danysantiago@google.com>
Date: Thu Feb 07 14:11:06 2019
Copy & Cast Parcelable array items when reading NavArgs from Bundle
Bundle#getParcelableArray() return Parcelable[] which NavArgs was
failing to cast to Item[] where Item is an app defined Parcelable. This
CL changes the generated code so that the Parcelable[] items are copied
into an array of the defined type in the nav graph.
This issue doesn't exist for Serializable since there is no
Bundle#getSerializableArray().
Bug: 123963545
Test: Manual test with sample app provided in issue.
Change-Id: Ibfcb703a1ad48929a0731b82a13a1f33813187b5
M navigation/safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/java/JavaTypes.kt
M navigation/safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/kotlin/KotlinTypes.kt
M navigation/safe-args-generator/src/tests/test-data/expected/java_nav_writer_test/MainFragmentArgs.java
M navigation/safe-args-generator/src/tests/test-data/expected/kotlin_nav_writer_test/MainFragmentArgs.kt
il...@google.com <il...@google.com> #4
This is fixed internally and will be available in Navigation 1.0.0-beta02
Description
Version used: 1.0.0-alpha11
Devices/Android versions reproduced on: irrelevant
<activity <== ACTIVITY AS DESTINATION
android:id="@+id/NewsDetailActivity"
android:name="co.bstorm.navtest.ui.news.newsdetail.NewsDetailActivity"
android:label="activity"
tools:layout="@layout/activity_news_detail">
<argument
android:name="model"
app:argType="co.bstorm.navtest.model.CustomModel[]" /> <== ARRAY OF PARCELABLES AS ARGTYPE
</activity>
When I use Activity as a destination, and have an argument argType as array of Parcelable custom model class, library crashes on destination inside safeArgs generated code when trying to extract the Parcelable array from Bundle:
2019-02-06 13:49:18.523 25348-25348/co.bstorm.navtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: co.bstorm.navtest, PID: 25348
java.lang.RuntimeException: Unable to start activity ComponentInfo{co.bstorm.navtest/co.bstorm.navtest.ui.news.newsdetail.NewsDetailActivity}: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to co.bstorm.navtest.model.CustomModel[]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to co.bstorm.navtest.model.CustomModel[]
at co.bstorm.navtest.ui.news.newsdetail.NewsDetailActivityArgs$Companion.fromBundle(NewsDetailActivityArgs.kt:25)
at co.bstorm.navtest.ui.news.newsdetail.NewsDetailActivity.onCreate(NewsDetailActivity.kt:13)
at android.app.Activity.performCreate(Activity.java:6915)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
The exact same setup works properly with the only difference being a Fragment as the destination, instead of activity, which once again flags this as definitely a library bug, and not something on my side.
Sample project:
Instructions:
1. Run app
2. Click on Settings in the upper right
3. App crashes when the destination Activity tries to un-bundle the parcelable array.
There is also a commented out action for the Fragment destination with the same setup which works properly.