Skip to content

Instantly share code, notes, and snippets.

@luckyhandler
Created September 25, 2016 21:26
Show Gist options
  • Select an option

  • Save luckyhandler/69460f2a4d014a3113e674f2f3f1fa55 to your computer and use it in GitHub Desktop.

Select an option

Save luckyhandler/69460f2a4d014a3113e674f2f3f1fa55 to your computer and use it in GitHub Desktop.
Android - Start Browser
public static void startBrowser(@NonNull final Context context, @NonNull final String url, @Nullable final Bundle animationBundle) {
final String preparedUrl = trimUrl(url);
final Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(preparedUrl));
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(browserIntent, animationBundle);
}
private static String trimUrl(final String url) {
String toReturn = url;
if (!url.startsWith("http://") && !url.startsWith("https://")) {
toReturn = "http://" + url;
}
if (url.endsWith("/")) {
toReturn = url.substring(0, url.length() - 1);
}
return toReturn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment