I have a MapView inside a fragment which creates a WebView and a loads URL.
The WebView loads fine if the MapView is hidden (android:visibility="gone"). But when it is made visible, the WebView's onPageStarted() is called but onPageFinished() is 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");
Few Observations:
If I leave the MapView hidden, WebView loads fine.
Only Log message after onPageStarted() - D/cr_Ime: [ImeAdapter.java:587] detach
Nothing in onReceivedError() or any other error callbacks.
I have done this way, it works fine for me.
I have 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 & onPageFinished both are getting triggered.
Screenshot:

Edit 1: Actually MapView always take focus while initialization so once MapView initialization process done request focus to WebView.
Hope this will help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With