Android: Start Activity from preferences.xml
Solution 1
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>
Solution 2
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>
Solution 3
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).
Solution 4
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"/>
Solution 5
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.
Lama
Updated on February 01, 2020Comments
-
Lama over 4 years
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 almost 13 yearsThanks 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)
-
Michael almost 13 yearsI've tried this code on my Samsung Galaxy S and it works fine. What Android version have you tried this on?
-
Lama almost 13 yearsI've tried it on AVD, all of the 7 API bethween version 4-10. I have no idea what could be the problem... :(
-
Lama almost 13 years...I'd like to ask you: please trie it with the
<PreferenceScreen>
tag. Maybe it works on your device too... -
Michael almost 13 yearsYes, it did. I changed names and replaced string references with strings but I don't think these changes made it work.
-
Lama almost 13 yearsThan, I really have no idea...:(
-
nmr over 10 yearsAs 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. -
Fred over 10 yearsThanks, you are right. You don't need any
<intent-filter>
. -
Morten Holmgaard almost 10 yearsKeep this in mind when using different buildtypes/productFlavors with gradle!
-
TWiStErRob almost 10 years@MortenHolmgaard that's it! But then how do you reference the package (for example:
buildTypes.debug.applicationIdSuffix = ".debug"
) -
Morten Holmgaard almost 10 years@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.
-
wsgeorge almost 9 yearsI'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.
-
Brais Gabin over 8 years@BoD please, update this answer pointing out that now the important package is the one in your gradle, not in your Manifest.
-
Eric about 8 yearsI just had the same problem, but I found a different solution. lets say my
Activity
is atcom.example.subpackage.Activity
...then theintent
element should look like<intent android:targetPackage="com.example" android:targetClass="com.example.subpackage.Activity"/>
-
Kornilov Ruslan over 6 yearsThis one is the best one. Worked for me.
-
Style-7 almost 5 years@Tim Stack I am not agree.
-
Tim Stack almost 5 yearsYou can't disagree, it's just how it is. An answer with code only is not considered a good answer and gets picked up by the review system to be reviewed for quality