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:
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
ororg.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.
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).