1

If anyone could please try this simple example and run the app I would really appreciate it. When the url loads, scroll a little bit down and try to click an orange button which says "TAKE ME THERE" there are two different behaviours in two different devices. In Nexus 5X it loads the destination in the same Webview but in Galaxy Nexus API 22 (Android 5.1.1) it opens the destination in the default android browser. I hope i was able to convey my problem. I tried searching for the solution but couldn't find it. Please HELP!!!!!!!!!!!!!!!!

package in.ac.cus.webview;

import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

private WebView myWebView;

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

    //Webview initial setup procedure
    myWebView = (WebView) findViewById(R.id.webview);
    WebSettings websettings = myWebView.getSettings();
    websettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("http://library.cus.ac.in");
    myWebView.setWebViewClient(new WebViewClient());
}

private class WebViewClient extends android.webkit.WebViewClient {
    @SuppressWarnings("deprecation")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
    }
    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        view.loadUrl(request.getUrl().toString());
        return true;
    }
}

}

user3304659
  • 59
  • 1
  • 1
  • 5
  • 1
    of course it does. what do you think `startActivity(intent);` does ? – njzk2 Apr 26 '17 at 18:09
  • I am actually new on android sir/mam so i am not able to find out the problem. But why there is two different behaviours in two different devices?? One opens in the same webview and another opens in default browser.. I want all the links to open within the same webview on all the android devices. Please help me sir/mam – user3304659 Apr 26 '17 at 18:13
  • Kindly check the below question http://stackoverflow.com/questions/42241452/ – sivaprakash Apr 26 '17 at 18:16
  • you may also want to check what `@TargetApi(Build.VERSION_CODES.N)` means. It is the part that is responsible for different behaviors on different os versions – njzk2 Apr 26 '17 at 18:19
  • Sir/mam even if I remove the line @TargetApi(Build.VERSION_CODES.N) it is still giving me the same result. I am so sorry for the trouble sir/mam but i am really suffering on this. – user3304659 Apr 26 '17 at 18:27
  • Thank you very much for helping. I think i have resolved it. I have removed the method public boolean shouldOverrideUrlLoading(WebView view, String url) and just implemented the method public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) and most probably turns out to be my solution. Thank you once again for your prompt response. – user3304659 Apr 26 '17 at 18:58

1 Answers1

1

The public booleanshouldOverrideUrlLoading(WebView view, WebResourceRequest request) method is targeted to android N, wich is 7.x.x In your other shouldOverrideUrlLoading method, you are asking to the system to handle the web, thas why it opens the phone browser. You should do the same in both methods. load the given url into de webView.

private class WebViewClient extends android.webkit.WebViewClient {
    @SuppressWarnings("deprecation")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        view.loadUrl(request.getUrl().toString());
        return true;
    }   
}
Jonathan Aste
  • 1,764
  • 1
  • 13
  • 20
  • Sir/mam even if I remove the line @TargetApi(Build.VERSION_CODES.N) it is still giving me the same result. – user3304659 Apr 26 '17 at 18:32
  • Thank you very much for helping. I think i have resolved it. I have removed the method public boolean shouldOverrideUrlLoading(WebView view, String url) and just implemented the method public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) and most probably turns out to be my solution. Thank you once again for your prompt response. – user3304659 Apr 26 '17 at 18:58
  • Eventually you will need to implement the other method, check the behavior of your WebViewClient on older versions, I'm not sure if the new method works fine for them. – Jonathan Aste Apr 26 '17 at 19:15
  • Please if this answer is the correct mark it as the Correct Answer. Thanks in advance, whatever you need just ask again :) – Jonathan Aste Apr 26 '17 at 19:15
  • I have kept the minimum api to 21. And removed the @targetAPI annotation and implemented the new shouldOverrideUrlLoading method and surprisingly it seemed like it gave me the result i wanted. But still need to test it throughly. Would definitely ask for your valuable advice and tips if required. Thank you very much sir. – user3304659 Apr 26 '17 at 19:22