可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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>
回答1:
When there are multiple activities
entries in AndroidManifest.xml, how
does android know which activity to
start first?
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.
I could not understand intent-filters.
Can anyone please explain.
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.
回答2:
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);
回答3:
First change the xml, mark your second activity as DEFAULT
<activity android:name=".AddNewActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Now you can initiate this activity using StartActivity method.
回答4:
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
回答5:
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.
<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.DEFAULT" />
</intent-filter>
</activity>
</application>
回答6:
Keep the first intent filter with keys MAIN
and LAUNCHER
and add another as ANY_NAME
and DEFAULT
.
Your LAUNCHER
will be activity A and DEFAULT
will be your activity B.
回答7:
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.
回答8:
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
回答9:
If possible try this one instant solution:
Intent intent =new Intent(getApplicationBaseContext,second_Act.class);
StartActivity(intent);