Android: Holding onto Tabs While Searching

Unlike my previous post, this one’s here to remind me of something that I’ve tried quite hard to find a way around – and still, haven’t.

So, here’s what I’ve been trying to do:
I’ve got this nice class which serves as the base for setting up all the tabs. Needless to say, this sub-classes the TabActivity class. And we shall call it the TabMain class. TabMain sets up 3 tabs using the TabHost, and specifies 3 distinct Intents as specs to associate 3 Activity sub-classes with the tabs. Hmm, all this sounds so awfully complicated. Let me just simplify things by putting in some code –

public class TabMain extends TabActivity
      public void onCreate(Bundle icicle)
          TabHost tabHost = getTabHost();       // The activity TabHost
          TabHost.TabSpec spec;                 // Resusable TabSpec for each tab
          Intent intent;                        // Reusable Intent for each tab
          // Create an Intent to launch an Activity for the tab
          intent = new Intent().setClass(this, Tab1.class);
          // Initialize a TabSpec for each tab
          spec = tabHost.newTabSpec("tab1")
          // Add the spec to the tabhost
          // Repeated for all the other tabs

With that out of the way, let me continue with the plot. So in the manner described above, I managed to have 3 tabs set up. They functioned perfectly. But then, in a moment of unforeseen inspiration, I decided to implement a search functionality on one of the tab Activity class. Just to clarify, this class was actually a sub-class of ListActivity since I was displaying a list of things in it. As the list size increased, it only seemed logical to provide a search functionality. I decided to use the search dialog approach, since I was targeting to have this app working on Android 1.6 devices as well.

An interesting part of setting up the ‘search configuration’ is directing the search query to an Activity class which is ‘searchable’. Making an Activity searchable primarily involves providing the following in its declaration in the Manifest file:

            <action android:name="android.intent.action.SEARCH" />
        <meta-data android:name=""

Now what this means, is that when the user invokes a search this particular Activity will be started by Android. In my ListActivity class, since I wanted the class to handle the search itself, I made the necessary changes to the Manifest file. With a simple invocation of onSearchRequested(),I tested that my ListActivity was successfully ‘configured’ to handle search queries.

But lo behold – as soon as I invoked the first search query, I realised there was an inherent problem. Since the search got directed back to the ListActivity class, so now it was effectively rendered independent of the Tab setup that I had so carefully configured in the TabMain class. End result: the tabs disappeared as soon as the user invoked a search. Mind you, there was absolutely no problem in the search results that were displayed.

And hence,  I realised the profound issue of working with Search from within a Tab. The search will have to be directed to another Activity, so your tabs will disappear.

So far I have understood that there are 2 ways to deal with this:

  1. Ignore the fact that the tabs disappeared, and expect that the user knows/figures-out that pressing the ‘back’ button will bring back the previous tabbed screen.
  2.  Implement a complex logic of switching tabs from within the tab Activity class. You’ll find many useful tips on how to do it on SO and Android dev blogs. By doing this, you’ll be able to quickly [as quick as the processor works] switch from the standalone Activity view [without the tabs] to the same tabbed Activity, and somehow pass the search results to be shown within the tab Activity.

And as complex as it sounds, it’s just as complicated to implement. Besides, if the user presses the ‘back’ on the handset, the standalone Activity without the tabs will be shown – quite confusing for the user!

For now, I’ve decided to stick to approach [a], until I find a more suitable solution. If I do find one, I’ll surely update this post.


One thought on “Android: Holding onto Tabs While Searching

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s