mapbox / mapbox/mapbox-maps-android

Custom MapView does not support onRestoreInstanceState

Open
#2,279 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

auto-triaged bug :beetle:
Dominant language
Kotlin
Stars
578
Forks
161
PR merge metrics
No merged PRs in 30d

Description

## Environment

- Android OS version: 11
- Devices affected: Samsung Galaxy A50
- Maps SDK Version: 11.1.0

Also tested on emulator with api 33

## Observed behavior and steps to reproduce

- Create custom MapView.
- Override onSaveInstanceState and onRestoreInstanceState.
- Create navigation from fragment with custom MapView to another fragment.
- Navigate to another fragment and return to fragment with MapView.

We get this stacktrace:
```
java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to com.example.mapboxbugs.CustomMapView$SavedState
at com.example.mapboxbugs.CustomMapView.onRestoreInstanceState(CustomMapView.kt:29)
at android.view.View.dispatchRestoreInstanceState(View.java:21559)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4001)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4007)
at android.view.View.restoreHierarchyState(View.java:21537)
at androidx.fragment.app.Fragment.restoreViewState(Fragment.java:698)
at androidx.fragment.app.Fragment.restoreViewState(Fragment.java:3176)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:3161)
at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:619)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:275)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1934)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1845)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1782)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:565)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7872)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
```

example code https://github.com/Copatych/MapboxBugs/tree/customMapView_onRestoreInstanceState

## Expected behavior

Custom MapView supports restoring InstanceState.

## Notes / preliminary analysis

## Additional links and references

example code https://github.com/Copatych/MapboxBugs/tree/customMapView_onRestoreInstanceState

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the custom MapView implementation in the linked MapboxBugs example, especially CustomMapView.kt and its onSaveInstanceState/onRestoreInstanceState methods, then reproduce the fragment navigation flow on Android. Compare the restored state received by the custom view with the expected SavedState type; done means returning to the fragment no longer throws the reported ClassCastException and instance state is restored.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.