In my android app, I wanted to start an activity 'B' from initial activity 'A'. I have created classes for both of these. However when using following code to start B, I get a runtime error: application has stopped unexpectedly, try again
. Here is my code:
Intent myIntent = new Intent(this, AddNewActivity.class);
startActivity(myIntent);
When I added a new entry in AndroidManifest.xml/manifest/application/activity/intent-filers
for activity B then the application worked.
I have two questions:
- When there are multiple activities entries in
AndroidManifest.xml
, how does android know which activity to start first? - I could not understand intent-filters. Can anyone please explain.
Here is my partial AndroidManifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ListAllActivity"
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=".AddNewActivity" 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>
When you create an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object. If multiple intent filters are compatible, the system displays a dialog so the user can pick which app to use.
An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent. Likewise, if you do not declare any intent filters for an activity, then it can be started only with an explicit intent.
According: Intents and Intent Filters
The
Activity
which you want it to be the very first screen if your app is opened, then mention it as LAUNCHER in the intent category and remaining activities mention Default in intent category.For example :- There is 2 activity A and B
The activity A is LAUNCHER so make it as LAUNCHER in the intent Category and B is child for Activity A so make it as DEFAULT.
Keep the first intent filter with keys
MAIN
andLAUNCHER
and add another asANY_NAME
andDEFAULT
.Your
LAUNCHER
will be activity A andDEFAULT
will be your activity B.