android / views-widgets-samples

Multiple samples showing the best practices in views-widgets on Android.
Apache License 2.0
5.06k stars 3.01k forks source link

ViewPager2 incompatible with MapView #117

Open badvok666 opened 4 years ago

badvok666 commented 4 years ago

Having these two Ui elements in the same view, calling onCreate(bundle) for the mapView, then navigating away and returning will cause:

java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class androidx.recyclerview.widget.RecyclerView$SavedState instead.

It is caused by both mapView and ViewPager using ViewCompat.generateViewId() and i guess somehow ending up with the same viewId. (Although i have no idea how)

Specifically for viewPager2 mRecyclerView.setId(ViewCompat.generateViewId()); is probably causing it.

This stack overflow's current solution is call ViewCompat.generateViewId() a bunch to take the view id's. Again that doesn't make sense to me since if they do both user ViewCompat.generateViewId() they should get different views. Maybe mapView does it manually and its their issue?

mattcrwi commented 4 years ago

I have the same issue. Thank you so much for finding this. The multi threaded nature of the error made tracking this down so hard.

peterdk commented 4 years ago

Wow, we have the same issue, and the 'fix' mentioned indeed does solve our issue. I spend 2 hours debugging this....

I assume that Maps somehow takes a ID of 0x1 statically. And that that's also the first ID generated by the ViewCompat.generateViewId() method. But I am speculating ofcourse. Really weird though to see a bug so specifically tied to our app design.

gaglileo commented 3 years ago

I have this bug too. I fixed with generating the bunch of ids, but I wait for an official fix.

sasasaiki commented 3 years ago

I have this bug too !!!! I spend 3 hours debugging this !!!! I was saved by this issue. Thank you. I hope it will be fixed officially .

binhbk230 commented 3 years ago

I have same issue, any can suggest me a solution?

sasasaiki commented 3 years ago

I have same issue, any can suggest me a solution? @binhbk230 It helped for me https://stackoverflow.com/a/59989710

binhbk230 commented 3 years ago

I have same issue, any can suggest me a solution? @binhbk230 It helped for me https://stackoverflow.com/a/59989710

Thank you so much, it work perfectly