Question

One of the features my app offers is to select something to be done in the background at search button long press. The activity that does that appears in the search long press options even if this feature is not activated in my app's settings screen. Is there a way to make my app's option not appear in the search long press options programmatically in this case?

This is how I define my activity in the manifest:

       <activity
            android:name="com.bill.deuterh.SearchButtonActivity"
             android:theme="@style/Theme.Transparent"
            android:label="@string/title_activity_search_button" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.DEFAULT"/>
             </intent-filter>
             <intent-filter>
                 <action android:name="android.intent.action.SEARCH_LONG_PRESS"/>
                 <category android:name="android.intent.category.DEFAULT"/>
             </intent-filter>
        </activity>
Was it helpful?

Solution

Split your <intent-filter> into 2 separate Activitys like this:

  <activity
        android:name="com.bill.deuterh.SearchButtonActivity"
         android:theme="@style/Theme.Transparent"
        android:label="@string/title_activity_search_button" >
         <intent-filter>
             <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.DEFAULT"/>
         </intent-filter>
    </activity>

    <activity-alias
        android:targetActivity="com.bill.deuterh.SearchButtonActivity"
        android:name="com.bill.deuterh.SearchButtonActivityAlias"
        android:enabled="false"
        android:label="@string/title_activity_search_button">
         <intent-filter>
             <action android:name="android.intent.action.SEARCH_LONG_PRESS"/>
             <category android:name="android.intent.category.DEFAULT"/>
         </intent-filter>
    </activity-alias>

You'll notice that the alias has enabled="false" which means that the default state of this Activity is disabled. In this state it won't show up in the search long press options list.

When you want to enable the search activity so that it shows up in the list, you just need to enable the alias component using the PackageManager, like this:

    PackageManager pm = getPackageManager();
    pm.setComponentEnabledSetting(new ComponentName(this,
            "com.bill.deuterh.SearchButtonActivityAlias"),
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);

If you want to disable it again, you can do it like this:

    PackageManager pm = getPackageManager();
    pm.setComponentEnabledSetting(new ComponentName(this,
            "com.bill.deuterh.SearchButtonActivityAlias"),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top