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 Prefe
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>
<Preference>
<intent
android:targetPackage="applicationIdFromBuildGradle"
android:targetClass="targetClassFromJavaFile.YourClassName"/>
</Preference>
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>
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"/>
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.
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.