Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preference items and explicit intents

I'm trying to call an activity from a Preference item

Ideally I'd like to simply specify an explicit intent in the xml for that Preference item

but my google fu has deserted me and I can only find examples of implicit intents, e.g.

<Preference android:title="@string/prefs_web_page" >
    <intent android:action="android.intent.action.VIEW"
            android:data="http://www.example.com" />
</Preference>

I have already called my activity elesewhere programatically, e.g.

Intent intent = new Intent(this, FileChooserActivity.class);

but I want to call this direct from the xml

is this possible or am I barking up the wrong tree?

like image 467
bph Avatar asked Dec 08 '22 21:12

bph


1 Answers

I believe the currently selected answer is not an explicit Intent, but an implicit one.
To make it an explicit Intent,

your preference XML should look like this (define targetPackage and targetClass):

<Preference android:title="@string/preference_title__intent_act" >
    <intent 
        android:targetPackage="com.mycompany.mypackage"
        android:targetClass="com.mycompany.mypackage.IntentActivityFromPreferences" />
</Preference>

Your activity declaration in AndroidManifest.xml should look like this:

<manifest 
    ...
    package="com.mycompany.mypackage">
    ...
    ...
        <activity 
            android:name="com.mycompany.mypackage.IntentActivityFromPreferences"
            android:label="@string/activity_intentactivityfromprefs_name">
        </activity>
</manifest>

Notes:

  • "com.mycompany.mypackage" is the root package declared in AndroidManifest.xml
  • If target class is not inside "com.mycompany.mypackage", it is also OK, just make sure that the declaration in AndroidManifest.xml is correct
like image 75
Dj S Avatar answered Jan 01 '23 13:01

Dj S