How to load url from Bundle in Android

I am unable to load the page using the link that is passed from the previous activity.

The code looks like this

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_webpage);

    webView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    /*Bundle b = getIntent().getExtras();
    String url = b.getString(DeviceDetails.URL_KEY);*/

    String url = getIntent().getStringExtra(DeviceDetails.URL_KEY);
    //String url = "http://google.pl/nexus/4";
    webView.setWebViewClient(new MyWebViewClient());
    webView.loadUrl(url);
}


private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

      

So when I use String url = "http://google.pl/nexus/4" everything looks fine. And I'm pretty sure my activity is getting the url from getIntent

because I was debugging it.

UPD1:

String inputUrl = detUrlEditText.getText().toString();
Intent intent = new Intent(DeviceDetails.this, ShowWebPageActivity.class);
Bundle extras = new Bundle();
extras.putString(URL_KEY, inputUrl);
intent.putExtras(extras);
startActivity(intent);

      

Previous activity. Passing the url is guaranteed because I debugged it. And the toast also shows the url passed in ShowWebPageActivity

.

+3


source to share


1 answer


Don't call view.loadUrl(url)

inside a method shouldOverrideUrlLoading()

.

I've seen examples elsewhere that do this and I don't understand why this is unnecessary.

shouldOverrideUrlLoading()

should return true when you pass in the url, and the WebView should not load it and return false when the WebView should continue to load the url.



You can call view.loadUrl()

if you decide that the WebView should open, for example, a different page than the URL parameter.

Here's an example of a subclass WebViewClient

:

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (handleWithSystemBrowser(url)) {
           Uri webpage = Uri.parse(url);
           Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
           if (intent.resolveActivity(getPackageManager()) != null) {
               startActivity(intent);
           }
           return true;   // tells WebView that we specifically handled the URL, so don't load it
        }

        return false;   // go ahead and load it in the WebView
    }

    private boolean handleWithSystemBrowser(String url) {

        // put your code here to check the URL
        // return true if you want the device browser to display the URL
        // return false if you want the WebView to load the URL
        .
        .
        .
    }
}

      

0


source







All Articles