-1
REINFORCEMENT I AM OPENING WEBVIEW IN A FRAGMENT AND NOT IN MAIN ACTIVITY - I want to open my site inside the webview, and the links from other sites should open outside, in the normal mobile browser, but this does not occur. My webview is inside a Fragment. You can help me?
MY CODE :
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_principal, container, false);
WebView webView = (WebView) v.findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
//se eu desabilito essa linha resolve, porem tudo abre fora do app, e
//queria somente que os links listados no site abrissem fora.
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://meusite.com/");
return v;
}
public class WebViewClientImpl extends WebViewClient {
private PrincipalFragment activity = null;
public WebViewClientImpl(PrincipalFragment activity) {
this.activity = activity;
}
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if(url.contains("https://meusite.com")) return false;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
}
Fragmente_principal.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_purple">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
I tried and does not solve, I am opening the webview inside a Fragment! If you have any interesting suggestions!
– Júnior Alencar
Any mistakes? Are you sure you put the classes in the right places? You put a Log. i() inside shouldOverrideUrlLoading to make sure that the method is called? Can you do a simple debug with Log. i, Log. d, etc? Do you know how to use break-point? Can you give more details? If you can notice the problem edit the question and be very clear where it failed.
– Guilherme Nascimento
the problem is that there are no errors, I just wanted my domain.com.br to stay inside the webview, and the links inside the site open outside the webview, when I do it outside of Facebook works... I’m doubtful where to put the method , because as said startActivity(i); gives error solutions are : add Qualifier Activity to method Creat Method Startactivity and ...
– Júnior Alencar
I have a public class Webviewclientimpl extends Webviewclient , it is within it that I will put the solution that informed me correct? the method I have already created in this class and in the Principalfragment class... but without success
– Júnior Alencar
public class Webviewclientimpl extends Webviewclient { private Principalfragment Activity = null; public Webviewclientimpl(Principalfragment Activity) { this.Activity = Activity; } @Override public Boolean shouldOverrideUrlLoading(Webview webView, String url) { if(url.contains("my.com")) Return false; Intent Intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); Activity.startActivity(Intent); Return true; } }
– Júnior Alencar
Try to intercept the links and treat them to open with a browser
– Murillo Comino
Principalfragment does not let use @ public View onCreateView(Layoutinflater Inflater, aNullable ViewGroup container, a*Nullable Bundle savedInstanceState) {

 View v = inflater.inflate(R.layout.fragment_principal, container, false);
 WebView webView = (WebView) v.findViewById(R.id.webview);
 webView.getSettings(). setJavaScriptEnabled(true);

 WebViewClientImpl webViewClient = new WebViewClientImpl(this);

 
 //webView.setWebViewClient(new WebViewClient());
 webView.loadUrl("https://meu.com/");

 return v;
– Júnior Alencar
@Murillocomino how do I do it buddy? I never thought about it!
– Júnior Alencar
Put the code in the question and not in the comments, post everything (removing data as passwords and addresses of Pis) because only with this can not be sure.
– Guilherme Nascimento
Okay, I’ll do it
– Júnior Alencar
I updated look if help, put my original code, I’ve tried several methods here stack, but either open everything inside my Fragment or open everything outside ... I want to thank everyone for the attention so far!
– Júnior Alencar
@Wouldn’t Guilhermenascimento have an if on Principalfragmente that checks if the URL is the same as on my site and if it isn’t open outside?? I just couldn’t structure it here...
– Júnior Alencar
@Júnioralencar do not know how this your code actually, you can have some 20 Fragments, subfragments, intents, etc. It can be anything there that failed, I use basically the same code, company links open in the webview, links from other sites open in the main browser of the user. It is likely that you are making some confusion. It came to put a Log inside
shouldOverrideUrlLoading
to see if it’s working?– Guilherme Nascimento
I don’t know how to put Log buddy, can you explain?
– Júnior Alencar
Well I just wanted a help, my code is so and as I explained it occurs this, if you prefer I can send you the code or the app if you want to see. Thanks in advance for the readiness to help! I apologize for not being so clear...
– Júnior Alencar
Just open out but then open all links when I comment on this line = webView.setWebViewClient(new Webviewclient()); Without comment everything opens inside the app. When I do in a normal Activity other than Fragmente, it works normal, but in this way disables the nav_s of my menu which is something I cannot change.
– Júnior Alencar
Junior your code is simply different from what I suggested at the beginning, do exactly like this: https://answall.com/a/143300/3635 and exchange
if(uri.getHost().equals("exemplo.com"){
and trade forif(uri.getHost().equals("meusite.com"){
, is just the site address, no file, folders or bars, just something likewww.meusite.com
, with nothing else.– Guilherme Nascimento
@Guilherme, I’ve tried this, the problem is that is giving err on this isOnline, on getApplicationContext, startActivity, setContentView, and on conexaofail , and all imports or changes that android studio suggests does not solve, because it creates methods that do not make the app run and yes generates other errors like the isOnline it generates a method or a getter that generate another error... and any treatment that having do the app nor open...
– Júnior Alencar
If you can explain to me what these variables are for, because in my app they appear as unused and that’s why the error occurs...
– Júnior Alencar