What's the correct syntax to define an activity in manifest file

前端 未结 7 1224
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-11 01:22

What is the correct way of adding an activity to AndroidManifest.xml?

Actually I have seen in many places an activity defined as



        
相关标签:
7条回答
  • 2020-12-11 01:57

    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

    0 讨论(0)
  • 2020-12-11 02:00

    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.
    0 讨论(0)
  • 2020-12-11 02:01

    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.

    0 讨论(0)
  • 2020-12-11 02:05

    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

    0 讨论(0)
  • 2020-12-11 02:05

    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" >
    
    0 讨论(0)
  • 2020-12-11 02: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

    0 讨论(0)
提交回复
热议问题