Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...