PDA

View Full Version : How to activate an arbitrary activity



sms2000
03-14-2011, 04:32 AM
Hi everyone!

I'm new to this forum and a newbie in Android development.

I have a couple of questions regarding Activities.

AFAIK the common method to activate an activity is like this:

Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);


But if I have say 100 different activities and need to programmically decide which to run I'd prefer to have some more generic code like:

String act_str = String.format ("Activity%03d", 123);
Class act_class = ConvertStringToActivityClass (act_str);
Intent intent = new Intent(this, act_class);
startActivity(intent);

How can I implement such a function "ConvertStringToActivityClass" ?

And one more question if you please.

How can I enumerate all the activities specified in the Manifest file (with parameters preferably)?

Thank in advance.

lockon_stratos
03-16-2011, 04:08 AM
you can use intent filter by declaring your activity in manifest similar to below code snippet:
<activity android:name=".Activity1">
<intent-filter>
<action android:name="android.test.activity1" />
<action android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Activity2">
<intent-filter>
<action android:name="android.test.activity2" />
<action android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Activity3">
<intent-filter>
<action android:name="android.test.activity3" />
<action android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
... and so on..

then when calling the activity you can put this way:

int activity_index = 1;
String s = "android.test.activity" + activity_index;
Intent intent = new Intent(s);
startActivity(intent);

sms2000
03-17-2011, 03:12 AM
Yea! That works.
Thanks!