Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView onPageFinished not called when MapView is visible

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:

  1. If I leave the MapView hidden, WebView loads fine.

  2. Only Log message after onPageStarted() - D/cr_Ime: [ImeAdapter.java:587] detach

  3. Nothing in onReceivedError() or any other error callbacks.

like image 748
Archie.bpgc Avatar asked Nov 09 '22 19:11

Archie.bpgc


1 Answers

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:

enter image description here

Edit 1: Actually MapView always take focus while initialization so once MapView initialization process done request focus to WebView.

Hope this will help you.

like image 56
Hiren Patel Avatar answered Nov 14 '22 23:11

Hiren Patel