|  |  | 
 |  |  | package com.dayu.pipirrapp.observer; | 
 |  |  |  | 
 |  |  | import android.util.Log; | 
 |  |  | import android.view.ViewGroup; | 
 |  |  | import android.webkit.WebView; | 
 |  |  |  | 
 |  |  | import androidx.annotation.NonNull; | 
 |  |  | import androidx.lifecycle.DefaultLifecycleObserver; | 
 |  |  | import androidx.lifecycle.LifecycleOwner; | 
 |  |  | 
 |  |  |  * 备注:地图页相关逻辑 | 
 |  |  |  */ | 
 |  |  | public class MapFragmenObserver implements DefaultLifecycleObserver { | 
 |  |  |     static String TAG = "MapFragmen"; | 
 |  |  |     WebView mWebView; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public void setmWebView(WebView webView) { | 
 |  |  |         mWebView = webView; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void onCreate(@NonNull LifecycleOwner owner) { | 
 |  |  |         DefaultLifecycleObserver.super.onCreate(owner); | 
 |  |  |         Log.i(TAG, "onCreate"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void onResume(@NonNull LifecycleOwner owner) { | 
 |  |  |         DefaultLifecycleObserver.super.onResume(owner); | 
 |  |  |         Log.i(TAG, "onResume"); | 
 |  |  |         if (mWebView != null) { | 
 |  |  |             mWebView.onResume(); | 
 |  |  |         } | 
 |  |  |         // 恢复 WebView,能正常执行网页的响应 | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void onPause(@NonNull LifecycleOwner owner) { | 
 |  |  |         DefaultLifecycleObserver.super.onPause(owner); | 
 |  |  |         Log.i(TAG, "onPause"); | 
 |  |  |         if (mWebView != null) { | 
 |  |  |             mWebView.onPause(); // 通过 onPause 动作通知内核暂停所有的动作,如 DOM 的解析、plugin 的执行、JavaScript 执行等 | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void onDestroy(@NonNull LifecycleOwner owner) { | 
 |  |  |         DefaultLifecycleObserver.super.onDestroy(owner); | 
 |  |  |         Log.i(TAG, "onDestroy>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | 
 |  |  |         if (mWebView != null) { | 
 |  |  |             ((ViewGroup) mWebView.getParent()).removeView(mWebView); | 
 |  |  |             mWebView.destroy(); // 当 Activity 要 destroy 时,应先将 WebView 移除,再 destroy 掉 | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |