[android] - avoid leaking dialog when context is being stopped

This commit is contained in:
tobrun
2019-01-04 19:57:15 +01:00
committed by Tobrun
parent 371d2e524f
commit 847c293a3e
3 changed files with 24 additions and 6 deletions

View File

@@ -45,6 +45,7 @@ public class AttributionDialogManager implements View.OnClickListener, DialogInt
@NonNull
private final MapboxMap mapboxMap;
private Set<Attribution> attributionSet;
private AlertDialog dialog;
public AttributionDialogManager(@NonNull Context context, @NonNull MapboxMap mapboxMap) {
this.context = context;
@@ -72,7 +73,7 @@ public class AttributionDialogManager implements View.OnClickListener, DialogInt
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.mapbox_attributionsDialogTitle);
builder.setAdapter(new ArrayAdapter<>(context, R.layout.mapbox_attribution_list_item, attributionTitles), this);
builder.show();
dialog = builder.show();
}
private String[] getAttributionTitles() {
@@ -93,6 +94,12 @@ public class AttributionDialogManager implements View.OnClickListener, DialogInt
}
}
public void onStop() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
private boolean isLatestEntry(int attributionKeyIndex) {
return attributionKeyIndex == getAttributionTitles().length - 1;
}

View File

@@ -69,6 +69,7 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
private NativeMapView nativeMapView;
@Nullable
private MapboxMap mapboxMap;
private AttributionClickListener attributionClickListener;
private MapboxMapOptions mapboxMapOptions;
private MapRenderer mapRenderer;
private boolean destroyed;
@@ -180,7 +181,7 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
mapboxMap.injectLocationComponent(new LocationComponent(mapboxMap));
// inject widgets with MapboxMap
attrView.setOnClickListener(new AttributionClickListener(context, mapboxMap));
attrView.setOnClickListener(attributionClickListener = new AttributionClickListener(context, mapboxMap));
// Ensure this view is interactable
setClickable(true);
@@ -379,6 +380,8 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
*/
@UiThread
public void onStop() {
attributionClickListener.onStop();
if (mapboxMap != null) {
// map was destroyed before it was started
mapGestureDetector.cancelAnimators();
@@ -731,7 +734,7 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
/**
/**
/**
* Set a callback that's invoked when the style has finished loading.
*
* @param listener The callback that's invoked when the style has finished loading
@@ -1219,11 +1222,19 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
@Override
public void onClick(View v) {
getDialogManager().onClick(v);
}
public void onStop() {
getDialogManager().onStop();
}
private AttributionDialogManager getDialogManager() {
AttributionDialogManager customDialogManager = uiSettings.getAttributionDialogManager();
if (customDialogManager != null) {
uiSettings.getAttributionDialogManager().onClick(v);
return uiSettings.getAttributionDialogManager();
} else {
defaultDialogManager.onClick(v);
return defaultDialogManager;
}
}
}

View File

@@ -538,7 +538,7 @@ public final class UiSettings {
*
* @return the active manager class used for showing attribution
*/
@NonNull
@Nullable
public AttributionDialogManager getAttributionDialogManager() {
return attributionDialogManager;
}