Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two launcher activities on phone, one on tablet

I am creating an application for Android 4.0+, and I want to achieve this behavior. Two different activities will have two launcher icons when the user's device is a phone, and it will be just one (the activities will consist of fragments that I will display as tabs in one main activity) on tablet devices. I know that one can set up multiple launcher activities in the manifest, but I think that maybe I would need something that will determine this operation during runtime (in java code).

like image 945
Sandra Avatar asked May 14 '13 08:05

Sandra


People also ask

Is it possible to have more than one launcher activity?

On latest Android versions - If there are more than one launcher activities and if we don't put this category tag then the activity which is mentioned first in the Android manifest file will get launched as start-up activity.

Can you have more than one launcher on Android?

Yes, You can have more than one launcher activity in your application. This will not create any kind of compile-time or run time error. You will find two launcher logos of your application in your device can launch different activities as we defined in manifest.

What is Android launcher in my activity?

Launcher Activities are the activities that can be launched for a given intent. For example, when you press an app icon on the home screen, the StartActivity intent starts the activity you have specified as the launcher activity.


1 Answers

Two different activities will have two launcher icons when the user's device is a phone, and it will be just one (the activities will consist of fragments that I will display as tabs in one main activity) on tablet devices.

There is no concept in Android of "phone" and "tablet". I am going to assume that you are distinguishing between "phone" and "tablet" in terms of screen size.

If that is true:

Step #1: Create a res/values/bools.xml file and define two <bool> resources, is_phone and is_tablet. Have is_phone be true and is_tablet be false.

Step #2: Create a res/values-.../bools.xml file, where ... is whatever qualifier you are using with your layouts to distinguish between "phones" and "tablets" (e.g., -large, -xlarge, -swNNNdp). Define the same two <bool> resources there with opposite values (i.e., is_phone is false, is_tablet is true).

Step #3: Add both activities to your manifest, each set up for the MAIN/LAUNCHER <intent-filter>. On the one you want to use on a "phone", add android:enabled="@bool/is_phone" to the <activity> element. On the one you want to use on a "tablet", add android:enabled="@bool/is_tablet" to the <activity> element.

This way, based on the same rules that you are using for your layouts, you will have a different launcher activity.


Apparently, this doesn't work, though I swear it used to.

Another option is to have a single activity be the MAIN/LAUNCHER one. Have it set up with android:theme="@style/Theme.NoDisplay", so it does not have a UI. Have it make the determination, in Java, in onCreate(), which of your "real" entry-point activities is appropriate for the given screen size, perhaps using the same bool resources I cited above. Have it then call startActivity() to pass control to the right activity and call finish() on itself (so the user does not encounter an invisible activity on the BACK stack). This technique is also used in cases where there is no way to control this via the manifest, such as "do we have Maps V1 or not" where you have android:required="false" on the <uses-library> element.

like image 160
CommonsWare Avatar answered Sep 21 '22 01:09

CommonsWare