views:

750

answers:

5

Hi i'm launching activity from preferences screen. Activity is shared among three preferences. I wonder if i can set extras for this activity in xml

<Preference
    android:key="action_1"
    android:title="@string/action_1_title"
>
    <intent
        android:action="com.package.SHAREDACTION"
    >

    </intent>
</Preference>

i wonder if i can do something like

<extras>
     <item
      android:name=""
      android:value=""/>
</extras>

All i need to do to pass an integer really. I can different actions and check action instead of extras.

+1  A: 

As your extras are not constants, you should pass them in the java code instead of xml.

Intent intent = new Intent( this, YourTargetActivity.class );
intent.putExtra( EXTRAS_KEY, extras );
yourPref.setIntent( intent );
kosokund
I know that, but i need to pass a value when selecting a preference defined in xml.
Alex Volovoy
hmm but you can retrieve a xml defined preference in the code also.
kosokund
Thomas - i'm going accept your answer - have to implement OnPreferenceClickListener, get the intent from preference and then add extra. Was kind of hoping that it can be done from xml - oh well.
Alex Volovoy
+1  A: 

There is a data field for intents described in the documentation here: http://developer.android.com/reference/android/content/Intent.html

It is used in the API demo application for the XML preferences to launch an intent in the Intent Preferences example.

Related example xml from that demo in preferences.xml:

    <PreferenceScreen
            android:title="@string/title_intent_preference"
            android:summary="@string/summary_intent_preference">

        <intent android:action="android.intent.action.VIEW"
                android:data="http://www.android.com" />

    </PreferenceScreen>

Maybe this approach could work for you?

Paul Fernhout
Worked great for me. Thanks!
ojrac
+1  A: 

I got an answer, you can use it like this:

<Preference
    android:key="xxx"
    android:title="xxx"
    android:summary="xxx">
   <intent android:action="xxx" >
         <extra android:name="xxx" android:value="xxx" />
    </intent>        

</Preference>
Nitromouse
A: 

Yea! <extra android:name="xxx" android:value="xxx" /> is working!!! Thanks a lot.

Albert