Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search creates new activities in TabActivity

Tags:

android

search

I want to develop a tabbed application on Android. At the same time, I want the search functionality to be present on some tabs. For this reason, I declared some activities in manifest file and added them to TabHost. But the problem is that when I make a search, it calls onCreate() method of the current activity which resided in tab content. What I want is to make searchManager call onNewIntent() method so that a new activity is not created and I can handle the search in existing activity. I'm posting my manifest and TabActivity source file to be more clear:

Part of manifest file:

  <activity 
   android:name="KarniyarikTabsWidget" 
   android:label="@string/app_name"
   android:theme="@android:style/Theme.NoTitleBar" 
   android:launchMode="singleTop">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
  <activity android:name="UrunTab"
   android:theme="@android:style/Theme.NoTitleBar"
   android:launchMode="singleTop">
   <intent-filter>
    <action android:name="android.intent.action.SEARCH" />
   </intent-filter>
   <meta-data android:name="android.app.searchable"
    android:resource="@xml/searchable" />
  </activity>
  <activity android:name="ArabaTab" android:theme="@android:style/Theme.NoTitleBar"
   android:launchMode="singleTop">
   <intent-filter>
    <action android:name="android.intent.action.SEARCH" />
   </intent-filter>
   <meta-data android:name="android.app.searchable"
    android:resource="@xml/searchable" />
  </activity>
  <activity android:name="GecmisTab" android:theme="@android:style/Theme.NoTitleBar"
   android:launchMode="singleTop">
  </activity>
  <activity android:name="HakkindaTab" android:theme="@android:style/Theme.NoTitleBar"
   android:launchMode="singleTop">
  </activity>

Tab Activity onCreate method:

public class KarniyarikTabsWidget extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("UrunTab")
         .setIndicator("Ürün",res.getDrawable(R.drawable.product))
         .setContent(new Intent(this, UrunTab.class));
        tabHost.addTab(spec);

        //Do the same for other tabs
        spec = tabHost.newTabSpec("ArabaTab")
          .setIndicator("Araba",res.getDrawable(R.drawable.car))
          .setContent(new Intent(this, ArabaTab.class));
        tabHost.addTab(spec);

        //Do the same for other tabs
        spec = tabHost.newTabSpec("GecmisTab")
          .setIndicator("Geçmiş",res.getDrawable(R.drawable.history))
          .setContent(new Intent(this, GecmisTab.class));
        tabHost.addTab(spec);

        //Do the same for other tabs
        spec = tabHost.newTabSpec("HakkindaTab")
         .setIndicator("Hakkında",res.getDrawable(R.drawable.about))
          .setContent(new Intent(this, HakkindaTab.class));
        tabHost.addTab(spec);
    }
like image 782
Behlül Avatar asked Jul 05 '10 05:07

Behlül


1 Answers

Not sure if you have found an answer to this, but I was having a related problem in that I was trying to have the search UI show up from a menu of a tabactivity (i.e., the search should show up irrespective of whichever tab is active), and it was not showing up. After quite a bit of research and reading the 'Search' article carefully, I noticed the following paragraph:

The search dialog is not, by default, available from every Activity of your application. Rather, the search dialog is presented to users only when they invoke search from a searchable context of your application. A searchable context is any Activity for which you have declared searchable meta-data in the manifest file. For example, the searchable Activity itself (declared in the manifest snippet above) is a searchable context because it includes meta-data that defines the searchable configuration. Any other Activity in your application is not a searchable context, by default, and thus, does not reveal the search dialog. However, you probably do want the search dialog available from your other activities (and to launch the searchable Activity when the user executes a search). You can do exactly that.

You can also control which activities provide search at a more granular level. To specify only an individual Activity as a searchable context, place the with the "android.app.default_searchable" name inside the respective element (rather than inside the element). While uncommon, you can also create more than one searchable Activity and provide each one in different contexts of your application, either by declaring a different searchable Activity in each element, or by declaring a default searchable Activity for the entire application and then overriding it with a element inside certain activities. (You might do this if you want to search different sets of data that cannot be handled by the same searchable Activity, depending on the currently open Activity.)

In short, you will have to have the search meta-data for the specific activity from which you are calling onSearchRequested(). Not only that, the meta-data in these other activities should have the name as default_searchable and mention the search activity, and not the xml searchable file, like so:

<meta-data android:name="android.app.default_searchable"
                   android:value=".SearchScreen" />

Noticed that you have the search meta-data wrong in two places.

like image 155
Samik R Avatar answered Nov 07 '22 01:11

Samik R