mapbox / mapbox/mapbox-navigation-android

Crash when use navigationview with Hilt

Open
#4,582 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug jira-sync-complete UI
Dominant language
Kotlin
Stars
651
Forks
321
PR merge metrics
No merged PRs in 30d

Description

**Android API:** 28
**Mapbox Navigation SDK version:** 1.6.1

### Steps to trigger behavior

1.We use hilt to inject ViewModel in our navigation fragment
2 .Call `NavigationView.onDestroy()` in `Fragment.onDestroyView()` to avoid memory leaks
```
@AndroidEntryPoint
NaviagationFragment: BaseNaviagationFragment(){

@HiltViewModel
private val viewModel by viewModels()

...
override fun onDestroyView() {
navigationView.onDestroy()
super.onDestroyView()
}
...

}
abstract BaseNaviagationFragment:Fragment(){...}

```
3. When we click back key, app crash.
4. Log as below

![image](https://user-images.githubusercontent.com/63035601/124876089-36dae580-dffc-11eb-9a28-14612b4cec6d.png)

### Expected behavior
Not crash
### Actual behavior
App crash.
### Reason
1: NaviagationView.onDestory will call `isChangingConfigurations`
```
private boolean isChangingConfigurations() {
try {
return ((FragmentActivity) getContext()).isChangingConfigurations();
} catch (ClassCastException exception) {
throw new ClassCastException("Please ensure that the provided Context is a valid FragmentActivity");
}
}
```
2. When we use Hilt to inject something. Hilt will change our fragment's parent class to its generated intermediate class. It will change the context of the LayoutInflater and so the context of its layout and child view
It will change our code like below
2.1 ``` NaviagationFragment extends Hilt_NavigationFragment...{} ```
2.2 ``` Hilt_NavigationFragment extends Fragment...{} ```
2.3
![image](https://user-images.githubusercontent.com/63035601/124877013-38f17400-dffd-11eb-90a0-bd3040937875.png)

![image](https://user-images.githubusercontent.com/63035601/124878473-b964a480-dffe-11eb-9aca-77d685fce8dd.png)

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 NavigationView.onDestroy(), especially isChangingConfigurations(), and reproduce the reported back-navigation crash using a Fragment with Hilt-injected ViewModel on Android API 28. Confirm the behavior with Mapbox Navigation SDK 1.6.1, then verify that destroying the view no longer crashes when Hilt changes the fragment context.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.