0

I have an Intent that opens a web page in FireFox on a Samsung tablet but it keeps opening a new tab each time. I've been using the putExtra() with an EXTRA_APPLICATION_ID and it previously did the trick and opened the page reusing the same browser tab but now its broke. I'm not sure if it makes a difference but the code below is runOnUiThread() as there is other processing that touches views. Any ideas would be appreciated.

        String pageName = "http://google.com";

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(pageName));
        intent.putExtra(Browser.EXTRA_APPLICATION_ID, getApplication().getPackageName());

        startActivity(intent);
glez
  • 1,170
  • 3
  • 16
  • 42

2 Answers2

0
Use http://developer.android.com/reference/android/provider/Browser.html#EXTRA_APPLICATION_ID to ensure that the same browser tab is reused for your application. For example:

Context context = widget.getContext();
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());

You could also specify the Intent.FLAG_ACTIVITY_CLEAR_TASK flag to your intent to open a browser. This will clear any currently open tabs.

Asteroid
  • 718
  • 7
  • 21
  • Thanks for the response. I'd rather avoid closing all tabs but that didn't work either. The code you posted looks like what I've been using and what has been working with the substitute of the application package name for the widget context. What would "widget" be referring to in your example? – glez Mar 16 '18 at 13:20
  • I'm running this code in response to a BLE scanner read so its run on the bluetooth call back thread. Don't know if that is problem or not. – glez Mar 16 '18 at 13:30
0

So I noticed that when the above code did not work my default application to open a URL on the Samsung pad was set as Samsung's Internet browser. I switched the default to FireFox or Chrome and the exact same code works again! Apparently there is something funky about Samsung Internet app that prevents reuse of tabs.

glez
  • 1,170
  • 3
  • 16
  • 42
  • Update - The latest Samsung browser on an S9 fixes this problem. I was testing on a S6 but did not try other versions. – glez Mar 18 '18 at 17:55