Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NavigationViewEventDispatcher remove navigation listeners in onDestroy #1013

Merged
merged 1 commit into from
Jun 19, 2018

Conversation

danesfeder
Copy link
Contributor

Closes #981

Because MapboxNavigation survives rotation, any listeners from the view must be removed before rotation or shutdown. This was not the case for the MilestoneEventListener and ProgressChangeListener being added through NavigationViewOptions. A memory leak would then result from the old listener instances.

@danesfeder danesfeder added bug Defect to be fixed. navigation-ui labels Jun 11, 2018
@danesfeder danesfeder added this to the 0.15.0 milestone Jun 11, 2018
@danesfeder danesfeder self-assigned this Jun 11, 2018
Copy link
Contributor

@devotaaabel devotaaabel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe remove all listeners in onStop?

@danesfeder
Copy link
Contributor Author

@devotaaabel none of the other listeners are added to MapboxNavigation so they shouldn't leak unless I'm mistaken

Copy link
Contributor

@Guardiola31337 Guardiola31337 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor comments.

@@ -42,6 +46,13 @@ void initializeListeners(NavigationViewOptions navigationViewOptions, MapboxNavi
assignInstructionListListener(navigationViewOptions.instructionListListener());
}

void onStop(MapboxNavigation navigation) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the @Nullable annotation?

navigation.addProgressChangeListener(navigationViewOptions.progressChangeListener());
ProgressChangeListener progressChangeListener = navigationViewOptions.progressChangeListener();
if (progressChangeListener != null) {
this.progressChangeListener = progressChangeListener;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move this assignment to L#69 like

this.progressChangeListener = navigationViewOptions.progressChangeListener();

and remove this line. It's safe because then we're checking if the listener is null in removeProgressChangeListener 👇

navigation.addMilestoneEventListener(navigationViewOptions.milestoneEventListener());
MilestoneEventListener milestoneEventListener = navigationViewOptions.milestoneEventListener();
if (milestoneEventListener != null) {
this.milestoneEventListener = milestoneEventListener;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here ☝️

@@ -290,4 +294,69 @@ public void onInstructionListShown() {

verify(instructionListListener, times(1)).onInstructionListVisibilityChanged(true);
}

@Test
public void onInstructionListHidden_listenerReturnsFalse() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can extract shared code between these new tests.

@danesfeder danesfeder force-pushed the dan-progress-leak branch 2 times, most recently from b5c9df4 to c059603 Compare June 15, 2018 18:00
@danesfeder danesfeder changed the title NavigationViewEventDispatcher remove navigation listeners in onStop NavigationViewEventDispatcher remove navigation listeners in onDestroy Jun 15, 2018
Copy link
Contributor

@Guardiola31337 Guardiola31337 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment not blocking the PR 🚀

@Test
public void onProgressChangeListenerAddedInOptions_isAddedToNavigation() {
NavigationViewEventDispatcher eventDispatcher = new NavigationViewEventDispatcher();
ProgressChangeListener progressChangeListener = mock(ProgressChangeListener.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1013 (comment)

Minor comment - what about 👇?

private ProgressChangeListener setupProgressChangeListener(NavigationViewOptions options) {
  ProgressChangeListener progressChangeListener = mock(ProgressChangeListener.class);
  when(options.progressChangeListener()).thenReturn(progressChangeListener);
  return  progressChangeListener;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Defect to be fixed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants