Книга: Beginning Android

Tabbed Browsing, Sort Of

Tabbed Browsing, Sort Of

One of the main features of the modern desktop Web browser is tabbed browsing, where a single browser window can show several pages split across a series of tabs. On a mobile device this may not make a lot of sense, given that you lose screen real estate for the tabs themselves.

In this book, however, we do not let little things like sensibility stop us, so let me demonstrate a tabbed browser, using TabActivity and Intents.

As you may recall from Chapter 10, a tab can have either a View or an Activity as its content. If you want to use an Activity as the content of a tab, you provide an Intent that will launch the desired Activity; Android’s tab-management framework will then pour the Activity’s user interface into the tab.

Your natural instinct might be to use an http:Uri the way we used a geo:Uri in the previous example:

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://commonsware.com"));

That way, you could use the built-in Browser application and get all of the features that it offers.

Alas, this does not work. You cannot host other applications’ activities in your tabs — only your own activities, for security reasons.

So, we dust off our WebView demos from Chapter 13 and use those instead, repackaged as Activities/IntentTab.

Here is the source to the main activity, the one hosting the TabView:

public class IntentTabDemo extends TabActivity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TabHost host = getTabHost();
  host.addTab(host.newTabSpec("one").setIndicator("CW")
   .setContent(new Intent(this, CWBrowser.class)));
  host.addTab(host.newTabSpec("two").setIndicator("Android")
   .setContent(new Intent(this, AndroidBrowser.class)));
 }
}

As you can see, we are using TabActivity as the base class, and so we do not need our own layout XML — TabActivity supplies it for us. All we do is get access to the TabHost and add two tabs, each specifying an Intent that directly refers to another class. In this case, our two tabs will host a CWBrowser and an AndroidBrowser, respectively.

Those activities are simple modifications to the earlier browser demos:

public class CWBrowser extends Activity {
 WebView browser;
 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  browser = new WebView(this);
  setContentView(browser);
  browser.loadUrl("http://commonsware.com");
 }
}
public class AndroidBrowser extends Activity {
 WebView browser;
 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  browser = new WebView(this);
  setContentView(browser);
  browser.loadUrl("http://code.google.com/android");
 }
}

They simply load a different URL into the browser: the CommonsWare home page in one (Figure 24-3), the Android home page in the other (Figure 24-4). The resulting UI shows what tabbed browsing could look like on Android.


Figure 24-3. The IntentTabDemo sample application, showing the first tab


Figure 24-4. The IntentTabDemo sample application, showing the second tab

Using distinct subclasses for each targeted page is rather wasteful. Instead we could have packaged the URL to open as an “extra” in an Intent and used that Intent to spawn a general-purpose BrowserTab activity, which would read the URL out of the Intent “extra,” and use that. The proof of this is left as an exercise for the reader.

Оглавление книги

Оглавление статьи/книги

Генерация: 1.456. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз