What does it mean when two or more activities, each having intent-filter with action=android.intent.action.ACTION_MAIN?

三世轮回 提交于 2019-12-11 06:37:24

问题


Doc says https://developer.android.com/reference/android/content/Intent.html#ACTION_MAIN is an entry point.

Example code:

 <activity android:name="org.A.A"
            android:theme="@style/NoTitle"
            android:screenOrientation="behind"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>

        <activity android:name="org.A.C"
            android:theme="@style/NoTitle"
            android:launchMode="singleTop"
            android:screenOrientation="behind">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>

        <activity android:name="org.A.B"
            android:theme="@style/NoTitle"
            android:launchMode="singleTop"
            android:screenOrientation="behind">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>

1) So using android.intent.action.ACTION_MAIN acts as an entry point to the parent component (parent component I mean the activity or receiver or service)?

2) If yes, Entry point from where, as there is no CATEGORY mentioned.


回答1:


Android apps are composed of different components. e.g. Activity, Service, BroadcastReceiver, and ContentProvider and each component can act as an entry point of the app.

Let's take activity as an example, you have defined an activity in your application with following action

<intent-filter>
     <action android:name="com.yourapp.SOME_ACTION" />
     <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

and I start an activity in my app with following intent.

Intent intent = new Intent("com.yourapp.SOME_ACTION"); // same action
startActivity(intent);

Now what will happen? System will search for activities with com.yourapp.SOME_ACTION action and if it finds one (in current scenario, it will be activity you have created with com.yourapp.SOME_ACTION in your app), it will start your app (if it is not already started) and will open the activity in your app.

See, now I can enter into your app by using Activity with com.yourapp.SOME_ACTION. Same thing happens in case of other components.




回答2:


Activity with MAIN action will be entry point of application.If you have one its well and good if more than one then you can have multiple activities through which you can enter into application.

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>

But if you have provided CATEGORY for activity it will create launcher for that entry point.

       <category android:name="android.intent.category.LAUNCHER" />

Suppose two activities is having both MAIN action and CATEGORY as launcher two app icon will be created one will have one activity as entry point second will have another one as entry point.

    <intent-filter>
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>


来源:https://stackoverflow.com/questions/41523925/what-does-it-mean-when-two-or-more-activities-each-having-intent-filter-with-ac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!