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:
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?
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?