Too many Activities being started when launching an Android Application
-
21-09-2019 - |
Question
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.
Solution
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>
OTHER TIPS
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.