Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 6ca0171

Browse files
author
Chris Yang
authored
[google_maps_flutter] Android: only destroy mapView once (#2772)
1 parent 999e11b commit 6ca0171

6 files changed

Lines changed: 99 additions & 10 deletions

File tree

packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.5.28+1
2+
3+
* Android: Make sure map view only calls onDestroy once.
4+
* Android: Fix a memory leak regression caused in `0.5.26+4`.
5+
16
## 0.5.28
27

38
* Android: Add liteModeEnabled option.

packages/google_maps_flutter/google_maps_flutter/android/src/main/java/io/flutter/plugins/googlemaps/GoogleMapController.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ final class GoogleMapController
7171
private final AtomicInteger activityState;
7272
private final MethodChannel methodChannel;
7373
private final GoogleMapOptions options;
74-
private final MapView mapView;
74+
@Nullable private MapView mapView;
7575
private GoogleMap googleMap;
7676
private boolean trackCameraPosition = false;
7777
private boolean myLocationEnabled = false;
@@ -534,6 +534,7 @@ public void dispose() {
534534
disposed = true;
535535
methodChannel.setMethodCallHandler(null);
536536
setGoogleMapListener(null);
537+
destroyMapViewIfNecessary();
537538
getApplication().unregisterActivityLifecycleCallbacks(this);
538539
}
539540

@@ -618,7 +619,7 @@ public void onActivityDestroyed(Activity activity) {
618619
if (disposed || activity.hashCode() != getActivityHashCode()) {
619620
return;
620621
}
621-
mapView.onDestroy();
622+
destroyMapViewIfNecessary();
622623
}
623624

624625
// DefaultLifecycleObserver and OnSaveInstanceStateListener
@@ -668,7 +669,7 @@ public void onDestroy(@NonNull LifecycleOwner owner) {
668669
if (disposed) {
669670
return;
670671
}
671-
mapView.onDestroy();
672+
destroyMapViewIfNecessary();
672673
}
673674

674675
@Override
@@ -891,6 +892,14 @@ private Application getApplication() {
891892
}
892893
}
893894

895+
private void destroyMapViewIfNecessary() {
896+
if (mapView == null) {
897+
return;
898+
}
899+
mapView.onDestroy();
900+
mapView = null;
901+
}
902+
894903
public void setIndoorEnabled(boolean indoorEnabled) {
895904
this.indoorEnabled = indoorEnabled;
896905
}

packages/google_maps_flutter/google_maps_flutter/example/android/app/build.gradle

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,24 @@ android {
5151
signingConfig signingConfigs.debug
5252
}
5353
}
54+
55+
testOptions {
56+
unitTests {
57+
includeAndroidResources = true
58+
}
59+
}
60+
61+
dependencies {
62+
testImplementation 'junit:junit:4.12'
63+
androidTestImplementation 'androidx.test:runner:1.1.1'
64+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
65+
testImplementation 'androidx.test:core:1.2.0'
66+
testImplementation "org.robolectric:robolectric:4.3.1"
67+
testImplementation 'org.mockito:mockito-core:3.2.4'
68+
testImplementation 'com.google.android.gms:play-services-maps:17.0.0'
69+
}
5470
}
5571

5672
flutter {
5773
source '../..'
5874
}
59-
60-
dependencies {
61-
testImplementation 'junit:junit:4.12'
62-
androidTestImplementation 'androidx.test:runner:1.1.1'
63-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
64-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.flutter.plugins.googlemaps;
2+
3+
import static org.junit.Assert.assertNull;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import android.app.Application;
7+
import android.content.Context;
8+
import androidx.lifecycle.LifecycleOwner;
9+
import androidx.test.core.app.ApplicationProvider;
10+
import com.google.android.gms.maps.GoogleMap;
11+
import io.flutter.plugin.common.BinaryMessenger;
12+
import io.flutter.view.FlutterMain;
13+
import java.util.concurrent.atomic.AtomicInteger;
14+
import org.junit.Before;
15+
import org.junit.BeforeClass;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
import org.mockito.Mock;
19+
import org.mockito.MockitoAnnotations;
20+
import org.robolectric.RobolectricTestRunner;
21+
22+
@RunWith(RobolectricTestRunner.class)
23+
public class GoogleMapControllerTest {
24+
25+
private Context context;
26+
private Application application;
27+
private GoogleMapController googleMapController;
28+
29+
@Mock BinaryMessenger mockMessenger;
30+
@Mock GoogleMap mockGoogleMap;
31+
@Mock LifecycleOwner lifecycleOwner;
32+
33+
@BeforeClass()
34+
public static void BeforeClass() {
35+
FlutterMain.setIsRunningInRobolectricTest(true);
36+
}
37+
38+
@Before
39+
public void before() {
40+
MockitoAnnotations.initMocks(this);
41+
context = ApplicationProvider.getApplicationContext();
42+
application = ApplicationProvider.getApplicationContext();
43+
googleMapController =
44+
new GoogleMapController(
45+
0, context, new AtomicInteger(1), mockMessenger, application, null, null, 0, null);
46+
googleMapController.init();
47+
}
48+
49+
@Test
50+
public void DisposeReleaseTheMap() throws InterruptedException {
51+
googleMapController.onMapReady(mockGoogleMap);
52+
assertTrue(googleMapController != null);
53+
googleMapController.dispose();
54+
assertNull(googleMapController.getView());
55+
}
56+
57+
@Test
58+
public void OnDestroyReleaseTheMap() throws InterruptedException {
59+
googleMapController.onMapReady(mockGoogleMap);
60+
assertTrue(googleMapController != null);
61+
googleMapController.onDestroy(lifecycleOwner);
62+
assertNull(googleMapController.getView());
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mock-maker-inline

packages/google_maps_flutter/google_maps_flutter/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: google_maps_flutter
22
description: A Flutter plugin for integrating Google Maps in iOS and Android applications.
33
homepage: https://github.com/flutter/plugins/tree/master/packages/google_maps_flutter/google_maps_flutter
4-
version: 0.5.28
4+
version: 0.5.28+1
55

66
dependencies:
77
flutter:

0 commit comments

Comments
 (0)