搜索在TabActivity中创建新活动

Beh*_*çar 8 search android

我想在Android上开发一个标签式应用程序.同时,我希望搜索功能出现在某些选项卡上.出于这个原因,我在清单文件中声明了一些活动并将它们添加到TabHost中.但问题是,当我进行搜索时,它会调用驻留在选项卡内容中的当前活动的onCreate()方法.我想要的是使searchManager调用onNewIntent()方法,以便不创建新的活动,我可以处理现有活动中的搜索.我发布我的清单和TabActivity源文件更清楚:

清单文件的一部分:

  <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>
Run Code Online (Sandbox Code Playgroud)

Tab Activity onCreate方法:

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);
    }
Run Code Online (Sandbox Code Playgroud)

Sam*_*k R 1

不确定您是否找到了答案,但我遇到了一个相关的问题,因为我试图从选项卡活动的菜单中显示搜索 UI(即,无论哪个选项卡处于活动状态,搜索都应该显示) ),但它没有出现。经过大量研究并仔细阅读“搜索”文章后,我注意到以下段落:

默认情况下,搜索对话框并非在应用程序的每个活动中都可用。相反,仅当用户从应用程序的可搜索上下文调用搜索时,才会向用户显示搜索对话框。可搜索上下文是您在清单文件中声明了可搜索元数据的任何活动。例如,可搜索活动本身(在上面的清单片段中声明)是可搜索上下文,因为它包含定义可搜索配置的元数据。默认情况下,应用程序中的任何其他活动都不是可搜索上下文,因此不会显示搜索对话框。但是,您可能确实希望其他活动可以使用搜索对话框(并在用户执行搜索时启动可搜索活动)。你完全可以这样做。

您还可以控制哪些活动提供更精细的搜索。要仅将单个 Activity 指定为可搜索上下文,请将名称为“android.app.default_searchable”的 放置在相应元素内(而不是元素内)。虽然不常见,但您还可以创建多个可搜索活动,并在应用程序的不同上下文中提供每个活动,方法是在每个元素中声明不同的可搜索活动,或者为整个应用程序声明默认的可搜索活动,然后使用某些活动中的一个元素。(如果您想要搜索同一可搜索活动无法处理的不同数据集,则可以执行此操作,具体取决于当前打开的活动。)

简而言之,您必须拥有您正在调用的特定活动的搜索元数据onSearchRequested()。不仅如此,这些其他活动中的元数据的名称应为 default_searchable 并提及搜索活动,而不是 xml 可搜索文件,如下所示:

<meta-data android:name="android.app.default_searchable"
                   android:value=".SearchScreen" />
Run Code Online (Sandbox Code Playgroud)

注意到您的搜索元数据有两个地方错误。