DOSKrupp
DOSKrupp

Reputation: 240

Using $ symbol in android:name value

I am trying to understand some piece of code in android. I see

<activity
android:name="Settings$DemoRangeActivity"

Could some one explain significance or usage of $ symbol in the name. I don't find any activity with name DemoRangeActivity. Is that correct what I am looking for?

Thanks

Upvotes: 1

Views: 124

Answers (1)

Halim Qarroum
Halim Qarroum

Reputation: 14093

Settings$DemoRangeActivity is a reference to the inner class DemoRangeActivity defined in the Settings class. If you noticed that this class is referenced in the manifest, but not actually defined in the Settings class, the app will crash as soon as this particular class will be opened as an Activity.

In the Settings application part of the AOSP, and more specifically in the Settings.java file, every activities related to the settings app are referenced at the bottom of the file with an empty body. They all extend the Settings class. This involves something such as :

/*
 * Settings subclasses for launching independently.
 */
public static class BluetoothSettingsActivity extends Settings { /* empty */ }
public static class WirelessSettingsActivity extends Settings { /* empty */ }
...

Each of these usually have an associated fragment implemented in a separate source file as you mentioned, which extends the SettingsPreferenceFragment class. This is done so that your fragment (implemented in the other file) can be separately launched as an activity. If you watch closely, the Settings class is a PreferenceActivity.

If you are adding your own fragment to the Settings app, you might want to declare it in the Settings.java file as well as in its manifest file. So basically, you'll have a DemoRange fragment in a separate file, as well as a DemoRangeActivity declared in both Settings.java and the manifest file, an example of such a declaration would be :

<!-- Demo range settings activity -->
<activity android:name="Settings$DemoRangeActivity">
   <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.DEFAULT" />
       <category android:name="com.android.settings.SHORTCUT" />
   </intent-filter>
   <!-- Here is your actual binding between the activity and the fragment -->
   <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
      android:value="com.android.settings.DemoRange" />
   <meta-data android:name="com.android.settings.TOP_LEVEL_HEADER_ID"
       android:resource="@id/demo_range_settings" />
</activity>

Upvotes: 1

Related Questions