Adding Menu Items Based on an Intent


Adding Menu Items Based on an Intent
Sometimes you'll want a menu item to launch an activity using an Intent (whether it's an activity in your application or another application). When you know the intent youwant to use and have a specific menu item that should initiate the intent, you can execute theintent with startActivity() during theappropriate on-item-selected callback method (such as the onOptionsItemSelected() callback).
However, if you are not certain that the user's devicecontains an application that handles the intent, then adding a menu item that invokes it can resultin a non-functioning menu item, because the intent might not resolve to anactivity. To solve this, Android lets you dynamically add menu items to your menuwhen Android finds activities on the device that handle your intent.
To add menu items based on available activities that accept an intent:
  • Define anintent with the category CATEGORY_ALTERNATIVE and/or CATEGORY_SELECTED_ALTERNATIVE , plus any other requirements.
  • Call Menu.addIntentOptions() . Android then searches for any applications that can perform the intentand adds them to your menu.

  • If there are no applications installedthat satisfy the intent, then no menu items are added.
    Note: CATEGORY_SELECTED_ALTERNATIVE is used to handle the currentlyselected element on the screen. So, it should only be used when creating a Menu in onCreateContextMenu() .
    For example:
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        super.onCreateOptionsMenu(menu);
    
        // Create an Intent that describes the requirements to fulfill, to be included // in our menu. The offering app must include a category value of Intent.CATEGORY_ALTERNATIVE.               
    //   :

    // Allowing your activity to be added to other menus

    // : intent , intent , the offering app   intent.category_alternative, menu 。   Intent intent = new Intent(null, dataUri);   intent.addCategory(Intent.CATEGORY_ALTERNATIVE); // Search and populate the menu with acceptable offering applications. menu.addIntentOptions( R.id.intent_group, // Menu group to which new items will be added 0, // Unique item ID (none) 0, // Order for the items (none) this.getComponentName(), // The current activity name null, // Specific items to place first (none) intent, // Intent created above that describes our requirements 0, // Additional flags to control items (none) null); // Array of MenuItems that correlate to specific items (none) return true; }

    For each activity found that provides an intent filter matching the intent defined, a menuitem is added, using the value in the intent filter's android:label as themenu item title and the application icon as the menu item icon. The addIntentOptions() method returns the number of menu items added.
    Note: When you call addIntentOptions() , it overrides any and all menu items by the menu group specified in the firstargument.
    Allowing your activity to be added to other menus
    You can also offer the services of your activity to other applications, so yourapplication can be included in the menu of others (reverse the roles described above).
    To be included in other application menus, you need to define an intentfilter as usual, but be sure to include the CATEGORY_ALTERNATIVE and/or CATEGORY_SELECTED_ALTERNATIVE values for the intent filtercategory. For example:
    <intent-filter label="@string/resize_image">
        ...
        <category android:name="android.intent.category.ALTERNATIVE" />
        <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
        ...
    </intent-filter>