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:
AndroidManifest.xml
, how does android know which activity to start first?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>
An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.
The intent filter specifies the types of intents that an activity, service, or broadcast receiver can respond. Intent filters are declared in the Android manifest file. Intent filter must contain <action>
An IntentFilter can match against actions, categories, and data (either via its type, scheme, and/or path) in an Intent. It also includes a "priority" value which is used to order multiple matching filters. IntentFilter objects are often created in XML as part of a package's AndroidManifest.
An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents.
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
<activity android:name=".HelloWorld" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:scheme="http" android:host="androidium.org"/> </intent-filter> </activity>
Launch HelloWorld
Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse("http://androidium.org")); startActivity(intent);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With