2010-07-05 60 views
8

我想在Android上開發選項卡式應用程序。同時,我希望在某些選項卡上顯示搜索功能。爲此,我在清單文件中聲明瞭一些活動並將它們添加到TabHost。但問題是,當我進行搜索時,它會調用駐留在選項卡內容中的當前活動的onCreate()方法。我想要的是讓searchManager調用onNewIntent()方法,以便不創建新的活動,並且我可以處理現有活動中的搜索。我張貼我的清單,並TabActivity源文件更清晰:清單文件的搜索在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> 

標籤活動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); 
    } 
+0

我認爲這兩個錯誤是相關的:http://code.google.com/p/android/issues/detail?id=17137 http://code.google.com/p/android/issues/detail? id = 4155 – Agos 2012-01-24 15:35:46

+0

你解決了這個問題嗎? – 2012-04-12 08:48:49

回答

1

不知道你是否已經找到了回答這個問題,但是我遇到了一個相關的問題,那就是我試圖從一個tabactivity菜單中顯示搜索UI(例如,無論哪個tab都處於活動狀態,搜索都會顯示出來),而且它沒有顯示向上。相當多的研究和仔細閱讀'Search' article後,我發現了以下內容:

搜索對話框不是在默認情況下,可從您的應用程序的每個活動。相反,只有當用戶從應用程序的可搜索上下文中調用搜索時,搜索對話纔會呈現給用戶。可搜索的上下文是您已在manifest文件中聲明瞭可搜索的元數據的任何Activity。例如,可搜索的Activity本身(在上面的清單片段中聲明)是一個可搜索的上下文,因爲它包含定義可搜索配置的元數據。默認情況下,應用程序中的任何其他活動都不是可搜索的上下文,因此不會顯示搜索對話框。但是,您可能確實希望通過其他活動提供搜索對話框(並在用戶執行搜索時啓動可搜索的活動)。你可以做到這一點。

您還可以控制哪些活動以更細化的級別提供搜索。要僅指定一個單獨的Activity作爲可搜索的上下文,請將「android.app.default_searchable」名稱放在相應元素(而不是元素內)中。雖然不常見,但您也可以創建多個可搜索的活動,並通過在每個元素中聲明不同的可搜索活動,或者爲整個應用程序聲明默認的可搜索活動,然後用某些活動中的元素。 (如果你要搜索的不同組不能由同一搜索活動,取決於當前打開的活動處理的數據,你可能做到這一點。)

總之,你必須有搜索的元您致電onSearchRequested()的具體活動的數據。不僅如此,元數據在這些活動應具備的名字default_searchable和提搜索活動,而不是XML搜索的文件,像這樣:

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

注意到,您在搜索元數據在兩個地方錯了。