14

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?

Community
  • 1
  • 1
Android
  • 3,828
  • 9
  • 46
  • 79
  • (.) is denoted that the launch a activity when the project is run if you are make(.) on all the activity then the when you are install .apk file at that time you will get more option menu in the your emulater or in the phone – Jalpesh Patel Jul 12 '12 at 11:04
  • 1
    See http://stackoverflow.com/questions/3608017/activity-name-in-androidmanifest-xml – ccheneson Jul 12 '12 at 11:11
  • tell me one simple thing what is the package name in which this activity resides??than i'll give the easiest solution to you.. – AkashG Jul 12 '12 at 11:14
  • @AkashG package name is com.example. both in same package – Android Jul 12 '12 at 11:15
  • ok.so you must have declared in manifest as package="com.example" at the top of manifest file under tag.right?? – AkashG Jul 12 '12 at 11:19
  • see i have answered your question.read it carefully u'll come to how we refer activities from different packages and concept of "dot". – AkashG Jul 12 '12 at 11:29
  • @BlackDevil see my answer, I fetched the android source code and you can understand how android name convention works in AndroidManifest. hope this what you are looking for :D – K_Anas Jul 12 '12 at 11:47

7 Answers7

11

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

Ilya Demidov
  • 3,275
  • 2
  • 26
  • 38
  • so putting a dot is correct way?? Is developer.android provide some documents for this – Android Jul 12 '12 at 11:07
  • hey, thanks for this hard work and make me understand. One last question and may foolish, but still. do the same thing apply for all activty. like if i have more then 10 activities then also it is prefixed by . – Android Jul 12 '12 at 11:28
  • If your activities there are on root package for each you should type prefix "." . For example if your package name is com.package and your Activity in com.package.activities.MainActivity then in Manifest you should type android:name=".activities.MainActivity". And ifthe second acitivty in package com.package.newactivities.SecondActivity then in Manifest it will be android:name=".newactivities.SecondActivity" – Ilya Demidov Jul 12 '12 at 11:32
5

yes putting the dot is right way.. if you see the eclipse self generated activity it looks like.

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

so its the right approach, our ide can understand

Ishtiaq
  • 1,206
  • 9
  • 18
4

Using relative paths is fine.
The path is separated by a dot, rather than a slash.

android:name=".one_path_level_down.MainActivity"
android:name=".one_path_level_down.DetailActivity"

The top level is the package level in your manifest file in the "package=". Something like the following:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abc.myapp1" >
david m lee
  • 2,547
  • 26
  • 14
3

We define package at the top under manifest tag for this purpose only that we do not have to declare it again and again if any activity resides it in the same package.We only start writing by dot to know that it belongs to the same package.All the activities residing in the same package will be accessed through this and if you declare new package other than com.example say com.example.sample than you only have to define .sample.YourActivityname.thats it.We do this refer activity from correct package.

hope this will help you.

AkashG
  • 7,868
  • 3
  • 28
  • 43
1

http://developer.android.com/guide/topics/manifest/activity-element.html#nm

android:name
The name of the class that implements the activity, a subclass of Activity. The attribute value should be a fully qualified class name

(such as, "com.example.project.ExtracurricularActivity"). However, as a shorthand, if the first character of the name is a period (for example, ".ExtracurricularActivity"), it is appended to the package name specified in the .

So given ApplicationManifest.xml:

...

then since android:name=".view.TaskListListView" has a leading period, so it is interpreted as android:name="com.stackoverflow.android.geotask.view.TaskListListView".

source

Community
  • 1
  • 1
Vinay W
  • 9,912
  • 8
  • 41
  • 47
0

just do like this, it means you activity in this package com.your.package we are mentioned MySecondActivitythis activity relates to that package

<activity 
     android:name="com.your.package.MySecondActivity" 
     android:label="@string/title_second_activity">
</activity>
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
0

the dot is not necessary but it basically means: the activity class lives in the same package of the app. So, if your app package is: com.your.package then:

  1. .YourActivity means that your class is inside com.your.package.
  2. YourActivity means that your class is inside com.your.package (same as above).

So it means this is basically the same thing

To confirm my answer look here at CesarB's answer, I had also fetch the android source coe and I totally agree with him, this is how the name convention work in the AndroidManifest:

  1. If the name starts with a dot, always prefix it with the package.
  2. If the name has a dot anywhere else(not at the beginning), do not prefix it.
  3. If the name has no dot at all, also prefix it with the package.
Community
  • 1
  • 1
K_Anas
  • 31,226
  • 9
  • 68
  • 81