I have two launcher in my app, one is a normal activity with the intent-filter for launcher/main and the other one is an activityAlias.
For both, I supply labels in the manifest via my strings.xml resources.
The reason for this is, that I want to provide different labels in different
locales.
Unfortunately only the label of my activity is changed accordingly to the
language selected in the android settings.
Why is that?
<activity
android:name=".MainActivity"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.Transparent">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
and the alias
<activity-alias android:name=".MainAlias"
android:enabled="true"
android:exported="true"
android:icon="#drawable/alias_icon"
android:label="#string/alias_label"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity-alias>
Related
What I have done is made a home screen application. Aka when you press home, it goes back to my app.
What I am struggling with though, is adding a normal launcher side to it, aka making it appear as an app in the apps menu. My intention is to create a simple screen that tells you and provides button access to change the home default settings. A wizard/setup guide in effect.
However, I am not sure how to have both together in one app, as it is something new to me.
The home bit works, it is the second activity that doesnt.
Manifest:
<activity
android:name=".HomeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:launchMode="singleTask"
android:theme="#style/FullscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".LauncherActivity">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Add the android.intent.action.MAIN inside the LauncherActivity Intent filter so it looks like this
<activity
android:name=".HomeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:launchMode="singleTask"
android:theme="#style/FullscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".LauncherActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
So, in a previous version of my Application, I had an entry point named MainActivity
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This version, I have a new entry point for my application, which then re-directs to MainActivity or another screen programmatically. Here are the two activities:
<activity
android:name=".NewEntryPoint"
android:label="#string/title_activity_second" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I removed the action.MAIN from main activity and set it to my NewEntryPoint. However, any users that had the application as a shortcut on their homescreen will lose the shortcut. Keeping the Launcher category in MainActivity didn't help either.
Is there a way to change entry point of my App without removing shortcuts?
Thanks!
I believe you could use activity-alias for this, but I would question whether or not it's worth the future maintenance just to avoid users having to add a shortcut to your app again. Something like this:
<activity-alias
android:name=".MainActivity"
android:targetActivity=".NewEntryPoint">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity-alias>
I would like to have two main activities in my app. So in my manifest I put:
<activity
android:name="mypackage1.MainActivity"
android:label="#string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="mypackage2.MainActivity2"
android:label="#string/title_activity_main2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Two icons are created in my apps menu. But when I click on each of them the first activity MainActivity is always launched. Is it possible to have two main activities? If so, what's wrong with what I did?
Thanks
The LAUNCHER intent filter is what determines what shows up in the app drawer/launcher. That is why you get two icons shown up.
However, you also set the DEFAULT intent filter, which sets the default Activity for the whole package. Since you set it twice, you get the problem of precedence of the first/latest registered. When you remove the DEFAULT filter, you will be able to start whatever you click on in the launcher.
In short, remove the following line from both Activities:
<category android:name="android.intent.category.DEFAULT" />
Yes, just mark two or more of your <activity>s as LAUNCHER within your manifest.
In addition you have to set the android:taskAffinity attribute on both of your Launcher-Activities which specify the exact package and Activity to be started.
<activity android:label="MyApp" android:name=".MyApp" android:taskAffinity="com.example.MainActivity">
<intent-filter>
<action android:name=".MyApp"/>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:label="Settings" android:name=".Settings" android:taskAffinity="com.example.SettingsActivity" >
<intent-filter>
<action android:name=".Settings"/>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Use android:documentLaunchMode="intoExisting", to launch a separate task based on the intent's Component name and data URI. Without this (by default), the activity will share all the same activities, as android:documentLaunchMode default to none.
intoExisting: The system searches for a task whose base intent's ComponentName and data URI match those of the launching intent. If the system finds such a task, the system clears the task, and restarts with the root activity receiving a call to onNewIntent(android.content.Intent). If the system does not find such a task, the system creates a new task. source
<activity
android:name=".CameraActivity"
android:exported="true"
android:documentLaunchMode="intoExisting"
android:label="#string/app_1_label">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ProfilePoseNetActivity"
android:exported="true"
android:documentLaunchMode="intoExisting"
android:label="#string/app_2_label">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The intention of potroae using task affinities to prevent your 2 activities from sharing the same task. However, it is annoying to have to select a task name, i.e. com.example/SettingsActivity for every task you want to launch separately.
I'm tearing my hair out over this one. I've got an application for which I use a short title to display in the launcher etc. I've also got a longer string that I use to display at the top of the screen from within the app (where there is more space). The observant will note that I do try to set NoTitleBar but not all devices appear to obey this style, which is why the long title must remain.
The problem I am having is that the label used on the app icon in the laucnher & desktop seemingly randomly reverts to the long string on some devices. On other devices everything is fine. I can't determine whether there are some artifacts lingering from an old version of the app, if its something I am doing wrong or if it's a bug with certain devices. The fact that its happening across multiple devices/vendors makes me tend to doubt the later.
My AndroidManifest consists of the standard stuff; an application tag containing a variety of activities. For reasons I dont understand, I was able to fix the problem on some devices by adding an android:label to all my IntentFilters.
This is basically what my AndroidManifest looks like:
<application android:label="#string/global_app_short_name" android:icon="#drawable/app">
<activity android:name=".HomeActivity"
android:launchMode="singleTask"
android:theme="#android:style/Theme.Black.NoTitleBar">
<intent-filter android:label="#string/global_app_short_name">
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".A1"
android:theme="#android:style/Theme.Black.NoTitleBar">
<intent-filter android:label="#string/global_app_short_name">
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
<activity android:name=".A2"
android:theme="#android:style/Theme.Black.NoTitleBar">
<intent-filter android:label="#string/global_app_short_name">
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
<activity android:name=".A3"
android:launchMode="singleTask"
android:theme="#android:style/Theme.Black.NoTitleBar"
android:windowSoftInputMode="stateHidden">
<intent-filter android:label="#string/global_app_short_name">
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
<activity android:name=".A4"
android:launchMode="singleTask"
android:theme="#android:style/Theme.Black.NoTitleBar">
<intent-filter android:label="#string/global_app_short_name">
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
<activity android:name=".A5"
android:theme="#android:style/Theme.Black.NoTitleBar">
<intent-filter android:label="#string/global_app_short_name">
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
<activity android:name=".A6"
android:theme="#android:style/Theme.Black.NoTitleBar">
<intent-filter android:label="#string/global_app_short_name">
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
<activity android:name=".A7"
android:theme="#android:style/Theme.Black.NoTitleBar">
<intent-filter android:label="#string/global_app_short_name">
...some stuff...
</intent-filter>
</activity>
</application>
I am confirming about creating activity.
My Manifest.xml is like this :
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".FirstActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ThirdActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can see property action android:name= property is "android.intent.action.MAIN" and
category android:name= is "android.intent.category.LAUNCHER" for all activities.
When the application starts up, it calls FirstActivity.
Then calls useless Activity such as ThirdActivity or SecondActivity.
In this case, is my manifest.xml correct?
Or, do I need to set another property to Second and Third activity?
If so, what is that?
I wonder manifest.xml file is right for my case.
Please advise.
Try this config:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".FirstActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" android:label="#string/app_name">
<intent-filter>
</intent-filter>
</activity>
<activity android:name=".ThirdActivity" android:label="#string/app_name">
<intent-filter>
</intent-filter>
</activity>
Think of an Intent as message used to start an Activity to do something. So I can create an Intent to view a web page and an application with an Activity which knows how to view a web page - most likely the browser - can intercept his Intent as act on it.
You tell Android which Activities can act on which Intents using the <intent-filter> part of your Manifest.
The MAIN Intent is a special one. This is sent to an application when it is launched and basically it says "Go!" So the Activity which shoud be displayed first needs to intercept this by having a correctly defined <intent-filter>.
As you had all three Activities with MAIN in their filter they all responded to the request to start your application. So you should have that <intent-filter> only for FirstActivity.
One of the other problems with using
<category android:name="android.intent.category.LAUNCHER" /> for more than one activity is that the Phone's launcher menu will display more than one icon...
From the docs:
CATEGORY_LAUNCHER The activity can
be the initial activity of a task and
is listed in the top-level application
launcher.