Tabs With Intents

If you look at the Android widgets and classes related to tabs, you’ll see that a tab can have either a View or an Activity as its contents. Most of the time, people use Views, as they are simpler to implement and lighter-weight to run. However, there may be circumstances where you feel putting your activities in tabs would be useful, so let’s take a look at how that is done, in a modified excerpt from Version 1.9 of The Busy Coder’s Guide to Android Development.

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. So, let’s create a tabbed Web browser.

Your natural instinct might be to use an http: Uri as the target of the Intents, one Intent per tab. That way, you could use the built-in Browser application and get all of the features that it offers. Alas, this does not work. It appears you cannot host other applications’ activities in your tabs, only your own activities, presumably for security reasons.

But, we can always use the WebView widget to provide a simplified browsing experience.

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

[sourcecode language=”java”]
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)));
}
}
[/sourcecode]

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 latter two activities are simple wrappers around WebView:

[sourcecode language=”java”]
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”);
}
}
[/sourcecode]

[sourcecode language=”java”]
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”);
}
}
[/sourcecode]

They simply load a different URL into the browser: the CommonsWare home page in one, the Android home page in the other.

The resulting UI shows what tabbed browsing could look like on Android:

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 this. The proof of this is left as an exercise for the reader.

About author

AndroidGuys
AndroidGuys 4641 posts

Founded on November 5, 2007, we've enjoyed bringing you the latest in Android news and rumors. Updated daily, we strive to deliver reviews, opinions, and updates on all things related to Android.

You might also like

News and Rumors

Teddy Tumble Proves Laundry Can be Fun, and Very Dizzying

It’s pretty much a fact that people hate doing laundry.  It’s also an unwritten rule that teddy bears are to be viewed only as cute and cuddly.  But what happens

News and Rumors

Dyzplatic Android Mini Collectibles Due Back in Stock

So you decided to grab yourself an Android collectible figure for yourself your friend but found they were no longer in stock. What the heck do you do now? Just be patient, that’s all. According to Dyzplastic, the highly-sought-after collectibles will be back in stock tomorrow. But wait, it gets better!

News and Rumors

Is Google Selling Out Net Neutrality? (UPDATED)

Google has long championed the ideals of Net Neutrality: the idea that users should have equal access to the internet, and that ISPs (and the government) should not prioritize some