Search code examples
androidandroid-activitysyntaxmanifest

What's the correct syntax to define an activity in manifest file


What is the correct way of adding an activity to AndroidManifest.xml?

Actually I have seen in many places an activity defined as

<activity 
    android:name="MyFirstActivity" 
    android:label="@string/title_first_activity">
</activity>

and in some places as

<activity 
    android:name=".MySecondActivity" 
    android:label="@string/title_second_activity">
</activity>

I just wanted to know if putting a dot(.) is the correct way of adding an activity to the manifest file.

I have gone through many posts but I didn't find an exact solution. This suggests the dot(.) is not required, while this suggests to use the dot(.). So what is the correct way?


Solution

  • dot means your package name. It's more short type of declaration.

    If you define a subclass, as you almost always would for the component classes (Activity, Service, BroadcastReceiver, and ContentProvider), the subclass is declared through a name attribute. The name must include the full package designation. For example, an Service subclass might be declared as follows:

    <manifest . . . >
         <application . . . >
             <service android:name="com.example.project.SecretService" . . . >
                 . . .
             </service>
             . . .
         </application> 
    </manifest>
    

    However, as a shorthand, if the first character of the string is a period, the string is appended to the application's package name (as specified by the element's package attribute). The following assignment is the same as the one above:

    <manifest package="com.example.project" . . . >
         <application . . . >
             <service android:name=".SecretService" . . . >
                 . . .
             </service>
             . . .
         </application> 
    </manifest> 
    

    When starting a component, Android creates an instance of the named subclass. If a subclass isn't specified, it creates an instance of the base class.

    http://developer.android.com/guide/topics/manifest/manifest-intro.html Declaring class names