1

I have an Activity (MainActivity) with a Navigation Drawer, and i change the main container with the appropriete Fragment every time a section (lets say i have tow: Tags and channels) is selected. I want to add a custom Search activity for each fragement, can i do that ?

I already did that with one fragment but the default searchable configuration is attached to the activity not the fragment. So how can i set a different Searchable Configuration to every fragement i have?

.MainActivity

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/AppTheme.NoActionBar" >
        <meta-data android:name="android.app.default_searchable"
            android:value=".TagSearchActivity" />

    </activity>

My Search Activities

     <activity android:name=".TagSearchActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable_tag"/>
    </activity>
    <activity android:name=".model.ChannelSearchActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable_channel"/>
    </activity>

To get the search service from the fragment i use this code

 SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    MenuItem searchItem = menu.findItem(R.id.search_tag);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    // Assumes current activity is the searchable activity
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
    searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
4

0 回答 0