android - Check activity alias into target activity -
i've got activity called a. want add 2 activity-alias called b , c. possible know if called b or c in code? want apply different behavior when it's called b or c.
you can provide additional information each <actvity-alias>
in manifest
, evaluate activityinfo
of packagemanager
:
to illustrate this, let's assume want display 2 textview
s in target activity
, set content depending on alias used.
in manifest
, put following elements:
<activity android:name=".halloactivity" android:label="@string/hallodefault" > </activity> <activity-alias android:name=".salutactivity" android:targetactivity=".halloactivity" android:label="@string/salutalias"> <meta-data android:name="locale" android:value="fr" /> </activity-alias> <activity-alias android:name=".helloactivity" android:targetactivity=".halloactivity" android:label="@string/helloalias"> <meta-data android:name="locale" android:value="en" /> </activity-alias>
to use alias, start activity
this:
intent intent = new intent(); string pname = getpackagename(); componentname componentname = new componentname(pname, pname + ".helloactivity"); intent.setcomponent(componentname); startactivity(intent);
then in oncreate()
method of halloactivity, android:label
, <meta-data>
this:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_hallo); string text; string label = "?"; string locale = "de"; int color; intent intent = getintent(); packagemanager pm = getpackagemanager(); try { activityinfo ai = pm.getactivityinfo(intent.getcomponent(), packagemanager.get_meta_data); label = getstring(ai.labelres); bundle b = ai.metadata; if (b != null) { locale = b.getstring("locale"); if (locale == null) { locale = "en"; } } } catch (exception ex) { log.e(tag, ex.getmessage()); } switch(locale) { case "en": text = "hello world :)"; color = color.blue; break; case "fr": text = "salut tout le monde :d"; color = color.red; break; default: text = "hallo zusammen ;)"; color = color.green; } textview tvhello = (textview) findviewbyid(r.id.tvhello); tvhello.settext(text); tvhello.settextcolor(color); textview tvlabel = (textview) findviewbyid(r.id.tvlabel); tvlabel.settext(label); }
important when working <activity-alias>
(quoted documentation):
with exception of targetactivity, attributes subset of activity attributes. attributes in subset, none of values set target carry on alias. however, attributes not in subset, values set target activity apply alias.
find out more <meta-data>
in documentation.
Comments
Post a Comment