Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 221
Android Avatar asked Jul 12 '12 11:07

Android


People also ask

Where do you declare activity in manifest?

Declaring an activity is a simple matter of declaring the <activity> element and specifying the name of the activity class with the android:name attribute. By adding the <activity> element to the Android Manifest, we are specifying our intention to include this component in our application.

Which tag is used to define activities in manifest file?

The manifest tag is the root tag of the XML file. Inside this tag are all other tags for your app's settings. Some of the following are the most common and important: uses-permission: This tag defines any permissions your app needs access to on the user's phone.

How do you declare activity?

To declare your activity, open your manifest file and add an <activity> element as a child of the <application> element. For example: <manifest ... > The only required attribute for this element is android:name, which specifies the class name of the activity.

Why do we have to declare activities in manifest file?

Because it lets you define the structure and metadata of your android application and its components.


1 Answers

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

like image 55
Ilya Demidov Avatar answered Oct 10 '22 15:10

Ilya Demidov