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

Viewed 70943

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

7 Answers

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.

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

Following is the complete list: 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" />

I realize "Android Developer Fundamentals Course". I see that there is a subtle difference when adding activities with nested elements and without them.

Default activity is defined this way:

<activity
    android:name=""
    android:label=""/>

When you add activity with closing tag (because there is nested element), you need move name attribute to opening tag:

<activity android:name="">
    android:label=""/>

    <meta-data/>

</activity>

On https://developer.android.com/guide/topics/manifest/manifest-intro

we can find that name attribute is mandatory:

The name of your subclass must be specified with the name attribute, using the full package designation.

Related