Android app with 2 launcher icons with separate taskAffinity - android

I have app with 2 launcher icons and 2 different Activities. Manifest file is as like below:
....
<activity
android:name=".MyActivity"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:taskAffinity="my.package.com.MyActivity">
<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=".MySettings"
android:label="#string/settings"
android:icon="#mipmap/ic_launcher"
android:taskAffinity="my.package.com.MySettings">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
....
I want MyActivity to be default screen to be opened after installing, so i used .category.DEFAULT.
action.MAIN and category.LAUNCHER are to show two separate launcher icons (1 for MyActivity, 1 for MySettings).
Problem: When i open MyActivity, MySettings screen is also gets opened. I want each launcher icon to open its corresponding Activity only. I have used taskAffinity for each Activity to solve problem but it did not work. I think to make taskAffinity work, Activity must be started with flag Intent.FLAG_ACTIVITY_NEW_TASK which i can not in my case (both are launch Activities).
I have also tried to use android:launchMode="singleTask" but it did not work either.
How to make each launcher icon to open only its Activity ?
UPDATE: Sorry, i realized MyActivity does not open MySettings, it seemed so though. MyActivity did not have UI, so it showed blank screen which is similar to MySettings. taskAffinity works correct. I have added android:theme="#android:style/Theme.NoDisplay" to MyActivity to hide UI.

Just use two different images for your apps to launch.
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
<activity
android:name=".MySettings"
android:label="settings"
android:icon="#drawable/ic_launcher2"
android:taskAffinity="my.package.com.MySettings">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

I solved that way:
<activity
android:name="andynaz.android.hyperfcalc.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>
<activity
android:name="andynaz.android.hyperfcalc.MainActivity2"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I did not use the taskAffinity, but I indicated the fully specified name of Activity classes (other than using 2 different images).

Related

android: what's the error?

I got this warning from console when running my application on my phone. And my phone haven't any response.
No Launcher activity found! [2016-03-11 14:44:59 - ActivityTest] The
launch will only sync the application package on the device!
Check this -
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You should set action main is caps
i.e <action android:name="android.intent.action.MAIN" />
I think your error comes because you declare this row in your Manifest wrong
<action android:name="android.intent.action.Main" />
Your Manifest must be some like this
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".FirstActivity"
android:label="This is FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/> <-- MAIN with upper case
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
This two lines in your Manifest always must look exactly like that.They are not classes in your app.They are actions that are happening.
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

How to determine which activity begins when i launch application?

I want know how i can determine which activity begins when i launch application ?
I have two Activity. MainActivity and Activity2. Which is the way i can code my application start every time in MainActivity ?
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity2"
android:label="#string/app_name"
android:screenOrientation="portrait"/>
<activity
Check out your AndroidManifest.xml file. In there you have each of your activities listed, then you can simply specify which one you'd like to be the default startup by adding this "intent-filter" shown below.
<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>

App widgets removed in home launcher if manifest activity intent-filter changed to different activity

I have an app with widgets. I created a new activity for a splash screen to show for a second and declared the new activity in my manifest with the intent-filter tags. I changed this:
<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>
to this:
<activity
android:name=".SplashScreen"
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=".MainActivity"
android:label="#string/app_name" >
</activity>
When I launched my app to the emulator, my widgets disappear from the home launcher and I have to re-add them. I want to add a splash screen to my app but I don't want my users to have to re-add their widgets, if possible. Any suggestions?

How to specify which activity starts on app launch?

I have an app with 3 different activities in it. When I launch the app one of the activities always starts first. But I want a differnt activity to start before the one that is currnetly starting first.
How would I change this to make a differnet activity start first?
Open your AnroidManifest.xml file and set the Launching Activity using the intent-filter tag as follows,
<activity android:name=".LaunchingActivity"
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 need to add an intent filter to the activity you want to start on application launch in your app's AndroidManifest.xml:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
You need to make changes in AndroidManifest.xml file...
<application
android:icon="#drawable/image"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name="define the activity which you want to start first here" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FirstActivity" >
</activity>
<activity android:name=".SecondActivity" >
</activity>
<activity android:name=".ThirdActivity" >
</activity>
</application>
Hope this will help you....
In your AndroidManifest.xml put the following:
<activity android:label="#string/app_name"
android:name=".TestActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
The intent-filter inside the activity tells Android which Activity to launch.

why does the menu button in the emulator show all the activities of my app?

i just want it to show only one activity on the main menu and hide the remaining ones.
My manifest file looks something like this.
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:debuggable="true">
<activity android:name=".MainAct" 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=".StartGame" 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=".Instructions" 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=".About" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
You don't need to repeat these lines for all activities;
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Just for the main one
(as an ad-on to your comments question:
From: http://developer.android.com/guide/topics/manifest/manifest-intro.html
The icon and label set for an intent
filter are used to represent a
component whenever the component is
presented to the user as fulfilling
the function advertised by the filter.
For example, a filter with
"android.intent.action.MAIN" and
"android.intent.category.LAUNCHER"
settings advertises an activity as one
that initiates an application — that
is, as one that should be displayed in
the application launcher. The icon and
label set in the filter are therefore
the ones displayed in the launcher.
Well, this is just a guess, but you have 2 intent filters in your manifest. If you only want one Activity for your entry point, you should only need the one intent filter for MAIN and LAUNCHER.

Categories

Resources