2

I have an Android SDK that uses a web view to show some pages, but depending on the requested link, I have it open an external browser. I am using the WebView's shouldOverrideUrlLoading method, and if it's a link to be opened externally, I use:

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

This works for us in a demo app, but someone integrating our SDK says that it's opening within the web view. I don't have access to their code, but what is the developer able to do on his side to prevent the default action to open an external browser? I have thought of two possible solutions that I found on SO:

  1. Use some flags to make sure no existing tasks are used, like so:

                Intent i = new Intent(Intent.ACTION_VIEW);
                Bundle b = new Bundle();
                b.putBoolean("new_window", true); //sets new window
                i.putExtras(b);
                i.addCategory(Intent.CATEGORY_BROWSABLE);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                i.setData(Uri.parse(url));
                startActivity(i);
    

Would this be sufficient?

  1. Another solution is to force opening in the chrome browser, like so:

    Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(urlString)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setPackage("com.android.chrome"); context.startActivity(intent);

What would be the preferable solution? Or am I missing anything else?

timetofly
  • 2,957
  • 6
  • 36
  • 76
  • "but someone integrating our SDK says that it's opening within the web view" -- it might, depending on what else is in your `shouldOverrideUrlLoading()` method. "Use some flags to make sure no existing tasks are used" -- that is unlikely to help, and I have no idea where you got that `new_window` bit from. "Another solution is to force opening in the chrome browser" -- that is unlikely to help, and it will fail on devices where Chrome is unavailable to the current user. "What would be the preferable solution?" -- find out why the reported behavior is occurring. – CommonsWare Jan 28 '18 at 18:29
  • @CommonsWare 1. I am limited in the fact that we don't have access to the app's code and don't want to bother other devs too much. How would you recommend debugging something like this? The reason I posted here is because I feel a bit stuck with this. 2. Why would force opening the chrome browser not likely to help? 3. I would assume the vast majority of Android users have either Chrome or, in case of an Amazon device, the "Silk" browser, no? – timetofly Jan 28 '18 at 18:34
  • "I am limited in the fact that we don't have access to the app's code" -- but the `shouldOverrideUrlLoading()` is in your own code, right? In particular, what are you returning? "don't want to bother other devs too much" -- without a reproducible test case, for all you know, the other devs are drunk. "How would you recommend debugging something like this?" -- you are going to need to figure out some sort of reproducible test case, presumably with the help of those other devs. For example, is the problem limited to some specific device model? If so, get your hands on one. – CommonsWare Jan 28 '18 at 19:25
  • "Why would force opening the chrome browser not likely to help?" -- for some reason, control is remaining with your app and is not transitioning to some other app. Saying "I want to start this specific browser" is unlikely to be an improvement over "I want to start any Web browser". "I would assume the vast majority of Android users have either Chrome or, in case of an Amazon device, the "Silk" browser, no?" -- most Android devices distributed in China will have neither, for example. The individual user might not have access to Chrome (e.g., I disable it and use Firefox Klar, personally). – CommonsWare Jan 28 '18 at 19:29
  • @CommonsWare "but the shouldOverrideUrlLoading() is in your own code, right?" correct. I am returning true or false based on if I started the new activity or not. The devs sent me the APK for their app and the issue is happening on my own test device, the same device I used with my test app that doesn't have the issue. That's why I'm led to believe it has something to do with the code on their end. You make a good point about the transition. Thank you for your thoughts. I'll add a bunch more debug logs and ask them to send me another apk. – timetofly Jan 28 '18 at 21:55
  • "the issue is happening on my own test device, the same device I used with my test app that doesn't have the issue" -- seriously? That is unexpected. I was betting on the problem being tied to some device manufacturer who had ed up `WebView`. My only guess now is that there is some flaw in the logic in `shouldOverrideUrlLoading()`, where you think that you are starting the browser for this situation, but due to some bug you are not. – CommonsWare Jan 28 '18 at 22:02
  • That's why I was at a loss. I will add debugging messages to check the logic of the rest of the method. Thanks. – timetofly Jan 28 '18 at 22:13

0 Answers0