Open NathanaelA opened 3 years ago
I have modified the code for both the Sound & Recenter button to be:
multiOnClickListener
to be a final
in the definitionModified this
private void clearListeners() {
setOnClickListener(null);
}
public void onDestroy() {
this.clearListeners();
multiOnClickListener.clearListeners();
}
Would you like a PR for this, this seems to fix my issues with at least this problem?
This allows the Object to be detached and re-attached to the view w/o it crashing. Because we don't clear the listeners, but just setOnClickeListener = null basically it allows everything to continue working when it is re-attached. Once the button is being destroyed, then we actually clear the listeners in the multiOnClickListener as they will no longer be valid...
Android API: Mapbox Navigation SDK version: 9.6.0 and Navigator 1.1.0 & 1.3.0
Steps to trigger behavior
Expected behavior
No Crash
Actual behavior
Crash
Appears to be like #1971, however the stack trace appears to be slightly different.
This occurs when I click the
Switch
application button (i.e. Square Nav Button); the app throws this error. I am logging all the events; and I see: The Pause & Stop events in sequence, which I then chain to the MapBox Navigator onPause, onStop calls exactly like the demo. However about 1/2 a second after; I get the below stack trace crash.... I've also tried not sending the onStop, onPause, etc... No change...Based on the callstack below; I suspect the issue is something has already called: https://github.com/mapbox/mapbox-navigation-android/blob/master/libnavigation-ui/src/main/java/com/mapbox/navigation/ui/RecenterButton.java#L142-L146
And https://github.com/mapbox/mapbox-navigation-android/blob/master/libnavigation-ui/src/main/java/com/mapbox/navigation/ui/RecenterButton.java#L144 This line I believe is actually invalid, as nothing in the code will re-create the MultiOnClickListener again in this element. as it is only created during creation of it, so if this element is re-attached it will misbehave and crash later: https://github.com/mapbox/mapbox-navigation-android/blob/master/libnavigation-ui/src/main/java/com/mapbox/navigation/ui/RecenterButton.java#L29
According to https://stackoverflow.com/a/65227045 & https://proandroiddev.com/make-your-custom-view-lifecycle-aware-its-a-piece-of-cake-90b7c0498686 it appears
onDetachFromWindow
is NOT the place to cleanup/null things like this, as the view can be re-attached without being re-created if used in things like tabs/pagers. So nulling the listener will lead to a crash if it is re-attached. In addition it might be good if you checked to verify that theMultiOnClickListener
isn't null in theclearListeners
function on line 143 as something obviously called this function a second time...