5

I have a very specific requirement: I need to open a URL link in a browser (not in a WebView), and I need it to open in the currently open tab, i.e. the opened page should be a "redirect" (meaning in the same tab).

Based on a consensus of multiple SO posts (see below), the way to do this is as follows:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, packageName);
startActivity(intent);

Specifically it is Browser.EXTRA_APPLICATION_ID that instructs the browser to open the link in the same tab:

enter image description here

Now in theory, this is supposed to work with all browsers.

Here it is important to note that packageName above needs to be the package name of the BROWSER that one wishes to open (e.g. com.android.chrome or org.mozilla.firefox), not the package name of your app.

Even though the documentation asks that you use the package name of your own app, it doesn't open the link in the currently open tab if you use the package name of your own app. Both Chrome & Firefox will open the link in a new tab.

So the trick to getting this to work is to set packageName as com.android.chrome, and voilà, Chrome opens the link in the currently open tab. Yay!

The same trick does not work with Firefox.

How can I make this work generally with any/all browsers?

Apparently this is a known issue with Firefox:

A. Firefox doesn't handle the flag Browser.EXTRA_APPLICATION_ID from an Intent.

B. Android Mozilla FireFox not opening page in a new tab.

Has this bug been fixed? Is there a known way to make Firefox open the link in the same tab?

How can I make this work for any/all browsers?

References:

1. Is there any way in Android to force open a link to open in Chrome?.

2. Android open browser from service avoiding multiple tabs.

3. Open an url in android browser, avoid multiple tabs.

4. Opening url in the last open tab in chrome.

5. Open url in same tab in browser using android code(not using webview).

6. Android app opening browser in the same tab?.

7. Browser.EXTRA_APPLICATION_ID not working in ICS.

8. How to Stop Multiple Tabs Opened in Browser.

9. How to Avoid Loading Local Page in New Tab on Default Android Browser.

10. Handle tab queues with Browser.EXTRA_APPLICATION_ID.

11. Allow loading URI Intents.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120

3 Answers3

5

It doesn't seem to be supported; AndroidManifest.xml shows the intent-filter ... and newTab = session == null and newTab = true are hard-coded. Unless this gets filed as a feature request - or an eventual PR, which enables this will get merged, this won't happen (and even then, it would only be supported in later versions).

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Thank you for your feedback, Martin. I'm still hopeful this is doable. If it works with Chrome, there just may be a way to make it work with Firefox. – Yash Sampat Dec 29 '19 at 13:09
  • @Y.S when the source code does not support that, how shall it pick up the `Intent`? Firefox is not magic... you could only use `WebView` & `GeckoView` for a consistent behavior (which is useless, when it shall be the default browser). Expecting identical behavior from the default browser is probably asked a little much. – Martin Zeitler Dec 29 '19 at 19:41
2

In the case of firefox try the below workaround by setting the component to intent.

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
 intent.setComponent(new ComponentName("org.mozilla.firefox","org.mozilla.firefox.App"));
 this.startActivity(intent);
  • This does not work. Moreover, you haven't included `Browser.EXTRA_APPLICATION_EXTRA_ID`, but even with that it does not work. – Yash Sampat Jan 01 '20 at 08:23
1

Firefox appears to have fixed this just fine.

Intent intent = new Intent();
intent.setPackage("org.mozilla.firefox");
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(link));
intent.putExtra(Browser.EXTRA_APPLICATION_ID, "MySpecialTab");

Note that it's better to use setPackage than setComponent as Mozilla has changed the actual activity name over time. The EXTRA_APPLICATION_ID is no such thing. If you want to reuse two tabs for many links call one "Interesting" and the other "Boring".

Renate
  • 761
  • 4
  • 11
  • Yeah, but Mozilla broke it when they changed from the "Fennec" platform to "Fenix". See: https://github.com/mozilla-mobile/fenix/issues/4106 Last Fennec version is here: https://download.mozilla.org/?product=fennec-latest&os=android&lang=multi – Renate Feb 02 '22 at 11:22