Start Android activity from list of activities

自闭症网瘾萝莉.ら 提交于 2020-01-03 04:38:08

问题


I am working on one android app, and in which i have one menu that shows list of Activity name.Upon clicking of any item from menu should start that specific Activity.

One way i know to do this is,

String classes[]= {"firstActivity","DetailActivity"}; intent i =new Intent(pkg_name+classes[position]);startActivity(i); 

where position=0 or 1. And for that i need to write in AndroidManifest.xml file below code for each Activity

<activity
        android:name="com.example.day1.DetailActivity"
         >
        <intent-filter>
            <action android:name="com.example.day1.DetailActivity" />
            <category android:name="android.intent.category.DEFAULT" />

        </intent-filter>
    </activity>

But i want just

<activity android:name=".DetailActivity"></activity>

simple code. Dont want to use intent-filter So please give me alternative i can use in this case.


回答1:


Or the other way round

Class classes[] = [ firstActivity.class, DetailActivity.class];
Intent i = new Intent(this, classes[position]);
startActivity(i);

Then, when you want to display the name of the activities, use classes[position].getSimpleName();




回答2:


Use this code :

Class chooseClass = null;
switch(position)
{
case 0 : chooseClass = firstActivity;
         break;
case 1 : chooseClass = DetailActivity;
         break;
}
if(chooseClass != null)
    startActivity(new Intent(context, chooseClass);



回答3:


Via code you can launch it with:

Intent i = new Intent(this, DetailActivity.class);
startActivity(i);

Now you don't need the filter.



来源:https://stackoverflow.com/questions/16564146/start-android-activity-from-list-of-activities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!