88import io .flutter .embedding .engine .plugins .activity .ActivityPluginBinding ;
99import io .flutter .plugin .common .BinaryMessenger ;
1010import 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 ;
1411
1512/**
1613 * GoogleApiAvailabilityPlugin
@@ -26,57 +23,53 @@ public GoogleApiAvailabilityPlugin() {
2623 }
2724
2825 @ 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+ }
3231
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+ }
3738
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+ }
4245
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+ }
4752
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+ }
5257
5358 @ Override
5459 public void onDetachedFromEngine (@ NonNull FlutterPluginBinding binding ) {
5560 unregisterPlugin ();
5661 }
5762
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-
7263 private void registerPlugin (Context context , BinaryMessenger messenger ) {
7364 methodCallHandler = new MethodCallHandlerImpl (context , googleApiAvailabilityManager );
7465 channel = new MethodChannel (messenger , "flutter.baseflow.com/google_api_availability_android/methods" );
7566 channel .setMethodCallHandler (methodCallHandler );
7667 }
7768
7869 private void unregisterPlugin () {
79- channel .setMethodCallHandler (null );
80- channel = null ;
70+ if (channel != null ) {
71+ channel .setMethodCallHandler (null );
72+ channel = null ;
73+ }
8174 }
8275}
0 commit comments