9

Possible Duplicate:
What's the “dot” for when registering an Activity

In all Android examples names of Activities, Services, etc. all start with a dot:

<activity android:name=".MyActivity" />

I forgot to do this in all Android projects - but they do work perfect.

My question: Is this leading dot really required?

EDIT: Here's a small snapshot example from one of my apps. This app works perfect. It doesn't use qualified names and it doesn't use dots:

<activity
        android:exported="false"
    android:name="Tankvorgaenge" >

    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>

    <meta-data
        android:name="android.app.searchable"
        android:resource="@xml/searchable" />
</activity>

<activity android:name="Tankvorgangdetails" />
<activity android:name="Tankvorgangdetailsbearbeiten" />
<activity android:name="TankvorgangUebersicht" />
<activity android:name="Verbrauch" />

<service android:name="MyService" />
Community
  • 1
  • 1
Harald Wilhelm
  • 6,656
  • 11
  • 67
  • 85
  • I noticed that using the older ADT/SDK it doesn't have a dot, but the newer do it automatically... from what I've seen. – EGHDK Aug 26 '12 at 15:56
  • From painful experience, I always use dot, not the full classname. This is after I had crashes in an app on some phones with certain API levels and not just API related, 2 HTC Desires, both 2.3. One crashed, one didn't! I've never found a reason for it. – Simon Aug 26 '12 at 16:37

4 Answers4

24

Omitting the dot and not fully qualifying the package/class name will work if and only if the specified class is not part of a subpackage within your application.

If your application package name is com.example.myapp, and you have an activity class com.example.myapp.MyActivity:

  1. android:name="MyActivity" will work.
  2. android:name=".MyActivity" will work.
  3. android:name="com.example.myapp.MyActivity" will work.

But if you have the same application package and an activity class in a subpackage within your source tree such as com.example.myapp.myactivities.MyActivity things change.

  1. android:name=".myactivities.MyActivity" will work
  2. android:name="com.example.myapp.myactivities.MyActivity" will work
  3. android:name="MyActivity" will not work
  4. android:name="myactivities.MyActivity" will not work

3 doesn't work because that will infer that the class name you mean is actually com.example.myapp.MyActivity like in the first example above. A class with this name won't be found and you'll get an error.

4 doesn't work because it looks like a fully qualified class name, that is the system will interpret it to mean that myactivities.MyActivity is the fully qualified name itself, not the real name of com.example.myapp.myactivities.MyActivity.

You need the leading dot here to clarify that you're using a relative path, not an absolute path. If you specify just a class name with no package info at all, the system infers that the class is at the root of your application package hierarchy.

adamp
  • 28,862
  • 9
  • 81
  • 69
2

The dot is to use the relative path to the app gives package_name. You can replace .MyActivity with com.yourActivityPackage.MyActivity

Brais Gabin
  • 5,827
  • 6
  • 57
  • 92
1

Yes, the activity[android:name] should either specify a fully qualified package.Class, or if it begins with a dot, then it is appended to the application's package.

See the doc

P Varga
  • 19,174
  • 12
  • 70
  • 108
0

It is required. It is an abbreviation for the package name. android:name is supposed to specify the full class name. By putting in a leading dot, we can indicate that the following class name is within the "package" declared above in the manifest.

Sameer
  • 4,379
  • 1
  • 23
  • 23