Android 如何以正确的方式将 Activity 添加到 manifest.xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20870202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:37:07  来源:igfitidea点击:

How to add Activity to manifest.xml in right way?

androidandroid-activityandroid-manifest

提问by Wesik

should I write each activity in android manifest and how? Must each activity have intent-filter, or not?

我应该在android清单中编写每个活动以及如何编写?每个活动是否必须有意图过滤器?

回答by Hamad

Multiple ways to add activites in Manifest file.

在清单文件中添加活动的多种方法。

intent filter is not a necessary tag for all activites,it is optional.

意图过滤器不是所有活动的必要标签,它是可选的。

Add Activity in application tag in your manifest:

在清单中的应用程序标签中添加活动:

 <!-- Main Activity-->
    <activity android:name=".YourActivityName" >
        <intent-filter>
      <!-- MAIN represents that it is the Main Activity-->
            <action android:name="android.intent.action.MAIN" />
      <!-- Launcher Denotes that it will be the first launching activity-->
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
 <!--Add Other activities like this-->
    <activity android:name=".YourActivityName2" >
 <!--Default Intent Filter-->
        <intent-filter>
            <action android:name="android.intent.action.DEFAULT" />
        </intent-filter>
    </activity>
 <!--OR Other activities like this And  intent filter is not necessary in other activites-->
    <activity android:name=".YourActivityName3" >
    </activity>
 <!--OR Add Other activities like this-->
    <activity android:name=".YourActivityName4" />

回答by vipul mittal

You must mention each activity in android manifest.

您必须在 android manifest 中提及每个活动。

Not all activity need intent filter. intent filters show when to launch this activity. usually you will have one activity with intent filter that is to show that it is first activity when application is launched.

并非所有活动都需要意图过滤器。意图过滤器显示何时启动此活动。通常你会有一个带有意图过滤器的活动,用于显示它是应用程序启动时的第一个活动。

inside application tag in your manifest:

在清单中的应用程序标签内:

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

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

回答by Dibyendu Mitra Roy

An activity must be mentioned inside an

必须在一个活动中提及

<activity>
    ...
</activity> 

tag. Each of the activity tags must be specified inside

标签。每个活动标签都必须在里面指定

<application>
    ...
</application> 

tag.

标签。

The default activity needs to have a

默认活动需要有一个

<intent-filter>
    ...
</intent-filter>

tag which will make the android system understand that this activity will be called at the time of App Launch.

标签,这将使 android 系统理解此活动将在应用启动时调用。

A can contain several attributes however, only the name attribute is mandatory.

A 可以包含多个属性,但是只有 name 属性是必需的。

Following is the complete list: https://developer.android.com/guide/topics/manifest/activity-element

以下是完整列表:https: //developer.android.com/guide/topics/manifest/activity-element

Default Activity Tag:

默认活动标签:

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

Other Activity Tag:

其他活动标签:

    <activity
        android:name=".SelectSubjectActivity"
        android:windowSoftInputMode="adjustResize" />

回答by Akshay Paliwal

only android:name="ActivtyName"is necessary.

只有android:name="ActivtyName"是必要的。

回答by user666

If you are using Eclipse ADT, when creating new Activity instead of creating a class create a Activity from New > Others... This way ADT automaticly adds your Activity to Manifest.

如果您使用的是 Eclipse ADT,则在创建新 Activity 而不是创建类时,从 New > Others 创建一个 Activity... 这样 ADT 会自动将您的 Activity 添加到 Manifest。

回答by Waqar Ahmed

You have to write entry in manifest for every activity and No intent filter is not necessary. You can simply write this :

您必须为每个活动在清单中写入条目,并且不需要意图过滤器。你可以简单地写这个:

 <activity
            android:name="com.example.chatter.List"
            android:label="@string/title_activity_list" >
        </activity>