19

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" />
        <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

1
  • I think that's the only way. As long as everything is done in the onCreate of this Dispatch activity including calling finish(), I don't think its too wasteful Nov 2, 2010 at 7:19

3 Answers 3

18

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.

1
  • Thanks for the great feedback. I will go with your first solution because I am sure most users will set their default to the ListView. Also, I don't fully grasp the second option, but first one sounds more than adequate. I will post an update in a week or two when I have it fully implemented.
    – Dave
    Nov 3, 2010 at 2:15
5

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();
0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.