mapbox / mapbox/mapbox-navigation-android
Crash when use navigationview with Hilt
Nobody has claimed this yet.
- 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

### 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


Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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