Can I launch different Activities on startup depending on user preferences?
I have a ListActivity and a MapActivity. I would like to launch either one of these activities on application startup that has been chosen by the user in a preferences window.
So far the only way I see to launch an activity on application startup is to specify it in the application manifest file using...
<activity android:name=".MyActiivty"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN开发者_StackOverflow" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I am thinking I might have to start an activity that does nothing but looks at the user preferences and then launches either the ListActivity or MapActivity. Seems like a waste to have an activity do nothing but launch another activity. In my research I have not found any solution to this problem. Any suggestions would be greatly appreciated.
Thanks & Regards, Dave
First, don't create some third activity. Just have the LAUNCHER
Activity
be either the list or the map, and have it call startActivity()
on the other one (plus finish()
) in onCreate()
before calling setContentView()
when needed. That way, ~50% of the time, you're launching the right activity.
In principle, you could have both activities have a LAUNCHER
<intent-filter>
, only enabling one. However, that will not work with desktop shortcuts, which will route to a specific activity (whichever one happened to be configured when they made the shortcut). If this does not concern you, you might go this route. However, try to test it with a few devices and custom home screens -- I'm not sure if everyone will pick up on your change immediately.
I just added the following code to the onCreate() method an it worked like a charm.
Intent intent;
intent = new Intent(this, MyMap.class);
startActivity( intent );
finish();
for new folks (me), following is dave's answer, plus changes i needed to make to AndroidManifest.xml.
Main activity:
Intent intent;
intent = new Intent(this, DisplayMessageActivity.class);
startActivity( intent );
changes to xml file, from -> http://developer.android.com/training/basics/firstapp/starting-activity.html
AndroidManifest.xml:
<activity
android:name="com.mycompany.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.mycompany.myfirstapp.MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfirstapp.MyActivity" />
</activity>
精彩评论