Search code examples
androidcallpreferencespreferenceactivity

How to get the key from a calling preference


This is part of my preferences.xml. One of these preferences will call the same activity, changePWEmail, when the user click on either one of them.

   <PreferenceCategory
        android:title="Security">
        <Preference
            android:title="Change email"
            android:summary="Option to change email"
            android:key="pref_security_email">
            <intent
                android:action="android.intent.action.MAIN"
                android:targetPackage="com.test"
                android:targetClass="com.test.changePWEmail"/>
        </Preference>
        <Preference
            android:title="Change password"
            android:summary="Option to change password"
            android:key="pref_security_password">
            <intent
                android:action="android.intent.action.MAIN"
                android:targetPackage="com.test"
                android:targetClass="com.test.changePWEmail"/>
        </Preference>
    </PreferenceCategory>

In the changePWEmail class, how do I get the key value (either "pref_security_email" or "pref_security_password") so that I can do appropriate action whether it is an email or a password change request? I'll appreciate any help. Thanks.


Solution

  •    <PreferenceCategory
            android:title="Security">
            <Preference
                android:title="Change email"
                android:summary="Option to change email"
                android:key="pref_security_email">
                <intent
                    android:action="com.example.action.email"
                    android:targetPackage="com.test"
                    android:targetClass="com.test.ChangePWEmailActivity"/>
            </Preference>
            <Preference
                android:title="Change password"
                android:summary="Option to change password"
                android:key="pref_security_password">
                <intent
                    android:action="com.example.action.password"
                    android:targetPackage="com.test"
                    android:targetClass="com.test.changePWEmailActivity"/>
            </Preference>
        </PreferenceCategory>
    

    In your activity:

    if (getIntent() != null)
      if ("com.test.action.email".equals(activtiy.getIntent().getAction())) {
    
      } else if ("com.test.action.password".equals(activtiy.getIntent().getAction())) {
    
      } else {
    
      }
    

    In your Manifest:

        <activity
            android:name="com.test.ChangePWEmailActivity" >
            <intent-filter>
                <action android:name="com.test.action.email" />
                <action android:name="com.test.action.password" />
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.Launcher" />
            </intent-filter>
        </activity>
    

    google for Android intents and interactions.