java - Add to menu using addIntentOptions providing multiple intents for a single activity -
i want use addintentoptions
drive menus when ever possible. seems cleanest way provide them. rather explicitly detailing activities, ask menu listing activities available data item.
so i'm trying put context menu listview
. works great. problem have activity has 2 intents consume data type, , first shows up.
the activity in question in androidmanifest.xml
<activity android:name=".ui.myactivity" android:label="the title"> <intent-filter android:label="first context label"> <action android:name="com.sample.action.first_action" /> <category android:name="android.intent.category.default" /> <category android:name="android.intent.category.alternative" /> <category android:name="android.intent.category.selected_alternative" /> <data android:scheme="myscheme" /> </intent-filter> <intent-filter android:label="second context label"> <action android:name="com.sample.action.second_action" /> <category android:name="android.intent.category.default" /> <category android:name="android.intent.category.alternative" /> <category android:name="android.intent.category.selected_alternative" /> <data android:scheme="myscheme" /> </intent-filter> </activity>
the code generate context menu
@override public void oncreatecontextmenu(contextmenu menu, view view, contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, view, menuinfo); uri uri = uri.fromparts("myscheme", getopaqueuriofselecteditem(view), null) intent intent = new intent(null, uri); intent.addcategory(intent.category_selected_alternative); // search , populate menu acceptable offering applications. menu.addintentoptions( 0, // menu group new items added 0, // unique item id (none) 0, // order items (none) this.getcomponentname(), // current activity name null, // specific items place first (none) intent, // intent created above describes our requirements 0, // additional flags control items (none) null); // array of menuitems correlate specific items // (none) }
as say, first intent of activity shows in context menu , behaves dream. don't see second intent, , see no reason shouldn't show up. if android allows 1 intent particular category per activity, that's pretty lame restriction.
i can see myself building dummy activity hands off myactivity
. that's clumsy , i'd avoid if possible.
edit: looking @ intent passed through activity context menu (or option menu, presumably), if both intents showed in menu, activity wouldn't have enough information tell intent selected, within activity getintent().getaction()
null.
this seems unfortunate oversight. surely isn't unusual have activity can consume type of data in more 1 way?
unless 1 of kind folk know i've missed, looks i'm going creating dummy activities.
edit: commonsware suggested, tried using queryintentactivityoptions
. added in code before menu.addintentoptions
in code above.
packagemanager pm = getpackagemanager(); final list<resolveinfo> available = pm.queryintentactivityoptions(this.getcomponentname(), null, intent, 0);
and in debugger found available
didn't include both of available intents myactivity
. issue isn't within addintentoptions
, it's deeper, within queryintentactivityoptions
somewhere.
my approach cannot work because queryintentactivityoptions()
, , methods call it, don't work in way needed approach.
for approach work, need result per intent-filter matched, result in multiple results per activity. also, need information intent-filter matched in result, action of intent-filter.
however queryintentactivityoptions()
doesn't find intent-filters, finds activities @ least 1 matching intent-filter. meaning 1 result per activity. result provides no information intent-filter matched intent.
this approach makes sense, it's shame doesn't allow activity provide multiple ways consume particular intent.
so workaround create fake activities activity more 1 action, hand off real activity.
so sample manifest included in question become
<activity android:name=".ui.myactivity" android:label="the title" /> <activity android:name=".ui.myactivityfirstaction" android:label="first action"> <intent-filter android:label="first context label"> <action android:name="com.sample.action.first_action" /> <category android:name="android.intent.category.default" /> <category android:name="android.intent.category.alternative" /> <category android:name="android.intent.category.selected_alternative" /> <data android:scheme="myscheme" /> </intent-filter> </activity> <activity android:name=".ui.myactivitysecondaction" android:label="second action"> <intent-filter android:label="second context label"> <action android:name="com.sample.action.second_action" /> <category android:name="android.intent.category.default" /> <category android:name="android.intent.category.alternative" /> <category android:name="android.intent.category.selected_alternative" /> <data android:scheme="myscheme" /> </intent-filter> </activity>
myactivityfirstaction , myactivitysecondaction call myactivity appropriate action , data.
i don't scheme much, still keeps actions in context menus defined in xml data rather in code, , allows me use addintentoptions()
.
i still consider addintentoptions()
tidy, , if commontasks tells me google have been backpedaling it, keep using until come across issues.
edit: commonsware suggests, possible create own library doing in non-hackish fashion. end more applications, move in direction (unless find existing method better :-) ).
Comments
Post a Comment