WebView onPageFinished is not called when MapView is displayed

I have a MapView inside a fragment that creates a WebView and loads a URL.

WebView loads fine if MapView hidden ( android:visibility="gone" ). But when it becomes visible, WebView onPageStarted() is called, but onPageFinished() never called.

MapView:

 <com.google.android.gms.maps.MapView android:layout_width="match_parent" android:layout_height="200dp" android:visibility="gone" android:id="@+id/mapView" /> 

WebView:

 webView = new WebView(getActivity()); webView.addJavascriptInterface(this, "scraper"); WebSettings webSettings = webView.getSettings(); webSettings.setUserAgentString("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0"); webSettings.setJavaScriptEnabled(true); webSettings.setDomStorageEnabled(true); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return true; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); System.out.println("page start called"); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); System.out.println("page finished called"); } }); webView.loadUrl("http://denver.craigslist.org/apa/5436947521.html"); 

A few observations:

  • If I left MapView hidden, WebView will load normally.

  • Message only after onPageStarted() - D / cr_Ime: [ImeAdapter.java∗87] disconnect

  • Nothing in onReceivedError() or any other callbacks.

+5
source share
2 answers

I did this, it works great for me.

I used MapFragment instead of MapView .

fragment_location.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> 

LocationFragment.java

 public class LocationFragment extends Fragment implements OnMapReadyCallback { private static View view; private GoogleMap googleMap; private double latitude = 23.0300, longitude = 72.5800; private WebView webView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { if (view != null) { ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) parent.removeView(view); } try { view = inflater.inflate(R.layout.fragment_location,null); MapFragment mapFragment = (MapFragment) getActivity().getFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); }catch (InflateException e) { /* map is already there, just return view as it is */ } return view; } @Override public void onMapReady(GoogleMap googleMap) { LatLng mLatLong = new LatLng(latitude, longitude); googleMap.setMyLocationEnabled(true); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLatLong, 13)); googleMap.addMarker(new MarkerOptions() .title("MyTitle") .snippet("MySnippet") .position(mLatLong)); if(webView!=null){ webView.requestFocus(); } } @Override public void onResume() { super.onResume(); webView = new WebView(getActivity()); webView.addJavascriptInterface(this, "scraper"); WebSettings webSettings = webView.getSettings(); webSettings.setUserAgentString("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0"); webSettings.setJavaScriptEnabled(true); webSettings.setDomStorageEnabled(true); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return true; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); Log.i("WebView", "page start called"); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); Log.i("WebView", "page finished called"); } }); webView.loadUrl("http://stackoverflow.com/"); } 

}

In my case, onPageStarted and onPageFinished both start.

Screenshot

enter image description here

Edit 1: In fact, MapView should always focus during initialization, so once the MapView initialization process made a focus request on the WebView .

Hope this helps you.

+2
source

Android Webview does not support the map, so it does not appear.

-1
source

Source: https://habr.com/ru/post/1244098/


All Articles