69

I would like to start an Activity from a default preferences.xml, with < intent > tag. The Activities are well tested, the problem is not with that. (I'm extending PreferenceActivity in my app, so the preferences.xml is "comes" with that) Please look at the code, what's wrong?

preferences.xml:

.... 
<PreferenceCategory 
    android:title="@string/titleEtcSetup">
    <PreferenceScreen
        android:key="renameCourses"
        android:title="@string/titleRenameCourses"
        android:summary="@string/textRenameDisplayedCoursesNames">
        <intent
             android:action="android.intent.action.VIEW"
             android:targetPackage="my.notifier.ui"
             android:targetClass="my.notifier.ui.EditCoursesNamesActivity" />         
    </PreferenceScreen>
.....
</PreferenceCategory>
..... 

manifest.xml:

....
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.notifier.ui"....
....
<activity android:name=".EditCoursesNamesActivity" android:label="@string/titleRenameCourses">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
 .....

The Activity isn't calling when I press the "renameCourses item", nothing happens. The LogCat is "clear", no errors or warnings. I was searching a lot, and I didn't find a solution, maybe I just missed something... Please help!

Lama
  • 1,313
  • 3
  • 11
  • 10

12 Answers12

67

I was having the same issue. I got this working by only declaring the action in my AndroidManifest.xml, as such:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.myapp" android:versionName="1.3" android:versionCode="4">

...

    <activity android:name=".activities.MyActivity" 
              android:label="@string/my_activity_title"
              android:theme="@android:style/Theme.Black.NoTitleBar">
        <intent-filter>
           <action android:name="com.example.myapp.activities.MyActivity" />
           <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
    </activity>

Then in my Preferences xml file:

<PreferenceCategory
        android:title="@string/my_activity_title">
    <PreferenceScreen
        android:title="@string/my_activity_title" 
        android:summary="@string/my_activity_title">
        <intent android:action="com.example.myapp.activities.MyActivity"/>
    </PreferenceScreen>
</PreferenceCategory>
Shygar
  • 1,213
  • 10
  • 10
  • 11
    As far as I can tell, any other app which responds to action `.activities.MyActivity` will also be eligible to field the started Intent. Which is sorta undesirable, particularly if you have Free/Pro versions of your app. – nmr Jan 03 '14 at 20:07
52

I believe <intent> tag must be inside <Preference>, not <PreferenceScreen>.

<PreferenceCategory 
    android:title="@string/titleEtcSetup">
    <Preference
        android:key="renameCourses"
        android:title="@string/titleRenameCourses"
        android:summary="@string/textRenameDisplayedCoursesNames">
        <intent
             android:action="android.intent.action.VIEW"
             android:targetPackage="my.notifier.ui"
             android:targetClass="my.notifier.ui.EditCoursesNamesActivity" />         
    </Preference>
.....
</PreferenceCategory>
Michael
  • 53,859
  • 22
  • 133
  • 139
  • Thanks for the post! But everything still the same in that way too. (btw: I used build-in API Demos at base to create the code, and it contains intent tags in PreferenceScreen) – Lama Sep 01 '11 at 21:24
  • I've tried this code on my Samsung Galaxy S and it works fine. What Android version have you tried this on? – Michael Sep 02 '11 at 11:16
  • I've tried it on AVD, all of the 7 API bethween version 4-10. I have no idea what could be the problem... :( – Lama Sep 02 '11 at 12:46
  • ...I'd like to ask you: please trie it with the `` tag. Maybe it works on your device too... – Lama Sep 02 '11 at 12:56
  • Yes, it did. I changed names and replaced string references with strings but I don't think these changes made it work. – Michael Sep 02 '11 at 15:21
  • Than, I really have no idea...:( – Lama Sep 02 '11 at 17:03
  • This one is the best one. Worked for me. – Kornilov Ruslan Mar 03 '18 at 21:16
35

Caution! The value of targetPackage should be the package id of the app, as declared in the root element of your AndroidManifest.xml file (which you define in your Gradle build file). It is not necessary the same as the Java package of your Activity class (people usually put them in a subpackage of "ui").

So in your specific case, I bet you the targetPackage should be "my.notifier", not "my.notifier.ui" (I would have to see the manifest to be sure).

BoD
  • 10,838
  • 6
  • 63
  • 59
  • 3
    Keep this in mind when using different buildtypes/productFlavors with gradle! – Morten Holmgaard Sep 06 '14 at 12:39
  • @MortenHolmgaard that's it! But then how do you reference the package (for example: `buildTypes.debug.applicationIdSuffix = ".debug"`) – TWiStErRob Sep 19 '14 at 21:25
  • @TWiStErRob you should reference your basepackage - to my knowledge you will get your buildtype specific file if you have a special one included, depending on you Bbuild Variant. – Morten Holmgaard Sep 21 '14 at 09:29
  • I'm using product flavours. android:targetPackage should be the applicationId NOT the package Id. Just thought we should be clear on the terminology so we don't confuse anyone. – wsgeorge Aug 08 '15 at 18:59
  • @BoD please, update this answer pointing out that _now_ the important package is the one in your gradle, not in your Manifest. – Brais Gabin Dec 02 '15 at 10:48
23

No need to add IntentFilter. You can refer to activity by fully qualified name:

<intent android:targetPackage="my.notifier.ui"
    android:targetClass="my.notifier.ui.EditCoursesNamesActivity"/>
dvikv90
  • 331
  • 2
  • 5
4

When I had this problem it was because I had made a sub-package for my activities. When I moved it into the root package the Preference screen could launch it.

Kieran
  • 860
  • 7
  • 12
  • I just had the same problem, but I found a different solution. lets say my `Activity` is at `com.example.subpackage.Activity`...then the `intent` element should look like `` – Eric Jun 12 '16 at 22:30
3

This solution shows you how to wire in an activity into your preferences headers.

First, your target activity must be declared in the manifest like this:

<activity android:name=".ui.activities.MyActivity">
    <intent-filter>
        <action android:name=".ui.activities.MyActivity" />
        <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Notice here that the android:name for the activity and action are the same.

Now in your preferences.xml file you need only to declare a header like this:

<header
    android:title="@string/my_activity_title"
    android:summary="@string/my_activity_summary">
    <intent android:action=".ui.activities.MyActivity"/>
</header>

And that's all there is to it.

Phil Haigh
  • 4,522
  • 1
  • 25
  • 29
3

I was having the same issue. and solve by this

androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.coding1.myapp">
<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            > 
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
// ==================== HERE ================================
        <activity
            android:name="yaran.AccountWorker.AuthenticatorActivity"
            android:label="@string/app_name"
            >
            <intent-filter>
                <action android:name="AuthenticatorActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
// ========================================================
        <service android:name="yaran.AccountWorker.AuthenticatorService" android:exported="false"  android:process=":auth">
            <intent-filter>
                <action android:name="android.accounts.AccountAuthenticator" />
            </intent-filter>
            <meta-data android:name="android.accounts.AccountAuthenticator"
                       android:resource="@xml/authenticator" />
        </service>
    </application>

and in Preference:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="General Settings" />

    <Preference
        android:key="account_settings"
        android:title="Account Settings"
        android:summary="">
        <intent
            android:action="AuthenticatorActivity"
            android:targetPackage="com.example.coding1.myapp"
            android:targetClass="yaran.AccountWorker.AuthenticatorActivity" />
    </Preference>
</PreferenceScreen>
Ali Bagheri
  • 3,068
  • 27
  • 28
2
<Preference>
    <intent
        android:targetPackage="applicationIdFromBuildGradle"
        android:targetClass="targetClassFromJavaFile.YourClassName"/>
</Preference>
Style-7
  • 985
  • 12
  • 27
1

I was able to fix this by changing the category in the intent filter from

android.intent.category.DEFAULT

to

android.intent.category.PREFERENCE

http://developer.android.com/reference/android/content/Intent.html#CATEGORY_PREFERENCE

<activity
  android:name=".ui.DatapackSelectionActivity"
  android:label="@string/app_name"
  android:screenOrientation="portrait"
  android:theme="@android:style/Theme.NoTitleBar" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.PREFERENCE" />
    </intent-filter>
</activity>

I guess if you want your action to be even more specific just remove the whole category node

<activity
  android:name=".ui.DatapackSelectionActivity"
  android:label="@string/app_name"
  android:screenOrientation="portrait"
  android:theme="@android:style/Theme.NoTitleBar" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
    </intent-filter>
</activity>
concept
  • 761
  • 2
  • 10
  • 16
1

targetPackage and targetClass(prefix) may differ because of refactoring package name. Easy way to check it you can delete activity in manifest and call startActivity() then you will see this error in logCat: "Unable to find explicit activity class {'targetPackage'/'targetClass'}". Those are the right names you should write into Preference>intent. Intent-filter is not needed.

yoo
  • 31
  • 3
0

I solved the same issue by declaring the <intent-filter> in the manifest as follows:

<activity
    android:name="PeriodosActivity"
    android:label="Periodos"
    android:screenOrientation="portrait">

    <intent-filter>
        <action android:name="Periodos" /> /*Same as in the preference's <intent> element's action attribute*/
        <category android:name="android.intent.category.DEFAULT"/>

</intent-filter>
Juan José Melero Gómez
  • 2,742
  • 2
  • 19
  • 36
0
<Preference>
<intent
    android:targetPackage="applicationIdFromBuildGradle"
    android:targetClass="targetClassFromJavaFile.YourClassName"/>

Please care use packgename in build.gradle(app)
applicationId "com.some.some"
my package was like this "com.some.love" in manifest and class I got exception while running .This Solved me
reference @style-7

lava
  • 6,020
  • 2
  • 31
  • 28