Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I launch different Activities on startup depending on user preferences?

Tags:

android

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

like image 277
Dave Avatar asked Nov 02 '10 04:11

Dave


People also ask

Where would we specify which activities should launch first in our app?

The intent-filter inside the activity tells Android which Activity to launch.

How do you start an activity only once the app is opened for the first time?

It is important to check that the first activity which opens when the app is launched is MainActivity. java (The activity which we want to appear only once). For this, open the AndroidManifest. xml file and ensure that we have the intent-filter tag inside the activity tag that should appear just once.

How do I use intent to launch a new activity?

The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo. class); startActivity(i);


2 Answers

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.

like image 172
CommonsWare Avatar answered Oct 15 '22 03:10

CommonsWare


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();
like image 32
dave Avatar answered Oct 15 '22 02:10

dave