8
8
import io .flutter .embedding .engine .plugins .activity .ActivityPluginBinding ;
9
9
import io .flutter .plugin .common .BinaryMessenger ;
10
10
import io .flutter .plugin .common .MethodChannel ;
11
- import io .flutter .plugin .common .PluginRegistry .Registrar ;
12
- import io .flutter .plugin .common .PluginRegistry .ViewDestroyListener ;
13
- import io .flutter .view .FlutterNativeView ;
14
11
15
12
/**
16
13
* GoogleApiAvailabilityPlugin
@@ -26,57 +23,53 @@ public GoogleApiAvailabilityPlugin() {
26
23
}
27
24
28
25
@ Override
29
- public void onAttachedToActivity (ActivityPluginBinding binding ) {
30
- methodCallHandler .setActivity (binding .getActivity ());
31
- }
26
+ public void onAttachedToActivity (ActivityPluginBinding binding ) {
27
+ if (methodCallHandler != null ) {
28
+ methodCallHandler .setActivity (binding .getActivity ());
29
+ }
30
+ }
32
31
33
- @ Override
34
- public void onDetachedFromActivity () {
35
- methodCallHandler .setActivity (null );
36
- }
32
+ @ Override
33
+ public void onDetachedFromActivity () {
34
+ if (methodCallHandler != null ) {
35
+ methodCallHandler .setActivity (null );
36
+ }
37
+ }
37
38
38
- @ Override
39
- public void onReattachedToActivityForConfigChanges (@ NonNull ActivityPluginBinding binding ) {
40
- methodCallHandler .setActivity (binding .getActivity ());
41
- }
39
+ @ Override
40
+ public void onReattachedToActivityForConfigChanges (@ NonNull ActivityPluginBinding binding ) {
41
+ if (methodCallHandler != null ) {
42
+ methodCallHandler .setActivity (binding .getActivity ());
43
+ }
44
+ }
42
45
43
- @ Override
44
- public void onDetachedFromActivityForConfigChanges () {
45
- methodCallHandler .setActivity (null );
46
- }
46
+ @ Override
47
+ public void onDetachedFromActivityForConfigChanges () {
48
+ if (methodCallHandler != null ) {
49
+ methodCallHandler .setActivity (null );
50
+ }
51
+ }
47
52
48
- @ Override
49
- public void onAttachedToEngine (FlutterPluginBinding binding ) {
50
- registerPlugin (binding .getApplicationContext (), binding .getBinaryMessenger ());
51
- }
53
+ @ Override
54
+ public void onAttachedToEngine (@ NonNull FlutterPluginBinding binding ) {
55
+ registerPlugin (binding .getApplicationContext (), binding .getBinaryMessenger ());
56
+ }
52
57
53
58
@ Override
54
59
public void onDetachedFromEngine (@ NonNull FlutterPluginBinding binding ) {
55
60
unregisterPlugin ();
56
61
}
57
62
58
- public static void registerWith (Registrar registrar ) {
59
- final GoogleApiAvailabilityPlugin plugin = new GoogleApiAvailabilityPlugin ();
60
- plugin .registerPlugin (registrar .context (), registrar .messenger ());
61
- plugin .methodCallHandler .setActivity (registrar .activity ());
62
-
63
- registrar .addViewDestroyListener (new ViewDestroyListener () {
64
- @ Override
65
- public boolean onViewDestroy (FlutterNativeView view ) {
66
- plugin .unregisterPlugin ();
67
- return false ;
68
- }
69
- });
70
- }
71
-
72
63
private void registerPlugin (Context context , BinaryMessenger messenger ) {
73
64
methodCallHandler = new MethodCallHandlerImpl (context , googleApiAvailabilityManager );
74
65
channel = new MethodChannel (messenger , "flutter.baseflow.com/google_api_availability_android/methods" );
75
66
channel .setMethodCallHandler (methodCallHandler );
76
67
}
77
68
78
69
private void unregisterPlugin () {
79
- channel .setMethodCallHandler (null );
80
- channel = null ;
70
+ if (channel != null ) {
71
+ channel .setMethodCallHandler (null );
72
+ channel = null ;
73
+ }
81
74
}
82
75
}
0 commit comments