Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open an url in android browser, avoid multiple tabs

Tags:

android

My project is mostly a web application. Now I have an android app with a home screen widget and would like to open a web page in the built-in browser on some user actions. That is easy. In MyAppWidgetProvider.onUpdate I create a new intent:

Intent intent = new Intent("android.intent.action.VIEW", 
    Uri.parse("http://my.example.com/"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.text, pendingIntent);

Unfortunately, a new browser window/tab is opened every time the user clicks the widget. My page contains some fancy AJAX/javascript running in the background and regularly sending http requests. So I end up having 8 tabs containing the same page and continuously sending 8-fold amount of http requests.

Is there any way, e.g. different action or additional parameters to avoid opening new tabs?

As a workaround I am now thinking about creating a new activity containing a WebView, but would like to avoid this complexity.

like image 296
geekQ Avatar asked Mar 30 '11 16:03

geekQ


People also ask

How to prevent user to open same URL in multiple Tab in same browser?

You cannot (and should not) do that. User can always just open use another browser or another computer to open another view onto the web site. So, basically you cannot ever prevent this. Your web-site should be able to handle multiple tabs viewing the same state.

Can you open multiple tabs on Android?

You can open as many tabs as you want in Chrome. You can also view all your tabs and switch between them.


1 Answers

Yes, this is possible. 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());
like image 76
Carter Maslan Avatar answered Oct 02 '22 13:10

Carter Maslan