In Android XML, what is the significance of a period (".") preceding a class or package reference?

StackOverflow https://stackoverflow.com/questions/23286515

سؤال

While browsing the Android developer docs I spotted example code where references to classes/packages are preceded by periods.

In the snippet below from http://developer.android.com/guide/topics/search/search-dialog.html there is ".SearchableActivity"

<application ... >
  <activity android:name=".SearchableActivity" >
    <intent-filter>
      <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
               android:resource="@xml/searchable"/>
  </activity>
  ...
</application>

Another example - http://developer.android.com//training/sharing/receive.html - has ".ui.MyActivity"

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

What do these preceding "." imply, and when/why should/would you use one? What happens if they are ommited?

هل كانت مفيدة؟

المحلول

android:name

Quoting docs

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 element. Once you publish your application, you should not change this name (unless you've set android:exported="false").

If in manifest you have

package="com.example.layout"

And your Activity is under the same package you can have

<activity
        android:name=".ActivityName" 

Instead of ShortHand You can also have

<activity
        android:name="com.example.layout.ActivityName" 

If the activity is not declared right in manifest you end up in ActivityNotFoundException

If your activity is in a different package then you mention the fully qualified class name

<activity
        android:name="packagename.ActivityName" 

Note: There is no default. The name must be specified.

نصائح أخرى

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity android:name=".DownloadActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

See the above example manifest file

Here "." means a package name

means following line

<activity android:name=".DownloadActivity" >

you can write 2 ways

<activity android:name="com.example.DownloadActivity" > and

<activity android:name=".DownloadActivity" > 

in your manifest file you already declare the base package name on top

package="com.example"

so android gives us a way to make it short

so if you use "." for class names it will take the package name that you have defined on top of the manifest file and system take it as com.example.DownloadActivity

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top