Is there any way to put extras to Intent from preferences?

AndroidAndroid IntentIntentfilterExtras

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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>

Solution 2 - Android

There is a data field for intents described in the documentation here.

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?

Solution 3 - Android

Add the preference to the preference.xml file:

<Preference android:title="user" android:key="user"/>            

And then you can use a setOnPreferenceClickListener to launch an Intent with extras.

Preference userButton = (Preference) findPreference("user");
userButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(Preference arg0) {
        Intent intent = new Intent(getActivity(), YourTargetActivity.class);
        intent.putExtra(EXTRA, mUser);
        startActivity(intent);
        return true;
    }
});

Solution 4 - Android

working for me.

<shortcut
    android:enabled="true"
    android:icon="@mipmap/xxx"
    android:shortcutDisabledMessage="@string/xxx"
    android:shortcutId="xxxx"
    android:shortcutLongLabel="xxx"
    android:shortcutShortLabel="xxx">
    <intent
        android:action="android.intent.action.VIEW"
        android:targetClass="xxx"
        android:targetPackage="xxx">
        <extra
            android:name="intent_name"
            android:value="true" />
    </intent>
</shortcut>

Solution 5 - Android

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 );

Solution 6 - Android

You can use

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

    <intent android:action="android.intent.action.VIEW"
            android:data="hello world" />

</PreferenceScreen>

to send the intent data. Then in your activity simply use:

getIntent().getDataString()

Solution 7 - Android

To send email or rate on market you need to use something like

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

    <intent android:action="android.intent.action.VIEW"
            android:data="market://details?id=com.your_package" />

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

    <intent android:action="android.intent.action.VIEW"
            android:data="mailto:[email protected]" />

</Preference>

Solution 8 - Android

Not really an answer to your question, but very much related. Maybe someone will find it useful. For newer API (>11) you have a preference-headers file and you can define custom intents for one of the headers. I was trying to add a custom Extra to one of the headers and the solution I found goes like this:

In your preference-headers.xml:

<header 
        android:fragment="com.mypackage.MyPreference$Prefs1Fragment"
	    android:title="Intent"
        android:summary="Launches an Intent.">
</header>

In your "MyPreference" class (extends PreferenceActivity) you have:

public static class Prefs1Fragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(getActivity(), MyTargetActivity.class);
        // set the desired extras, flags etc to the intent
        intent.putExtra("customExtra", "Something that I used to know");
        // starting our target activity
        startActivity(intent);
        // ending the current activity, which is just a redirector to our end goal
        getActivity().finish();
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex VolovoyView Question on Stackoverflow
Solution 1 - AndroidludwigmView Answer on Stackoverflow
Solution 2 - AndroidPaul FernhoutView Answer on Stackoverflow
Solution 3 - Androidpatrick-fitzgeraldView Answer on Stackoverflow
Solution 4 - AndroidGautamView Answer on Stackoverflow
Solution 5 - AndroidtbruyelleView Answer on Stackoverflow
Solution 6 - AndroidkylarsturnView Answer on Stackoverflow
Solution 7 - AndroidViktor YakuninView Answer on Stackoverflow
Solution 8 - AndroidAndyView Answer on Stackoverflow