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
The intent-filter inside the activity tells Android which Activity to launch.
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.
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);
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With