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>
There can be no two Lancher AFAIK. Logcat is a usefull tool to debug and check application/machine status in the behind. it will be automatic while switching from one activity to another activity.
There is no "first". In your case, with your manifest as shown, you will have two icons in your launcher. Whichever one the user taps on is the one that gets launched.
There is quite a bit of documentation on the subject. Please consider reading that, then asking more specific questions.
Also, when you get "application has stopped unexpectedly, try again", use
adb logcat
, DDMS, or the DDMS perspective in Eclipse to examine the Java stack trace associated with the error.If possible try this one instant solution:
intent filter is expression which present in manifest in your app that specify the type of intents that the component is to receive. If component does not have any intent filter it can receive explicit intent. If component with filter then receive both implicit and explicit intent
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.
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.
AndroidManifest.xml
Launch HelloWorld
First change the xml, mark your second activity as DEFAULT
Now you can initiate this activity using StartActivity method.