Android: Start Activity from preferences.xml

后端 未结 12 1975
轮回少年
轮回少年 2020-12-04 13:16

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

相关标签:
12条回答
  • 2020-12-04 13:38

    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>
    
    0 讨论(0)
  • 2020-12-04 13:40
    <Preference>
        <intent
            android:targetPackage="applicationIdFromBuildGradle"
            android:targetClass="targetClassFromJavaFile.YourClassName"/>
    </Preference>
    
    0 讨论(0)
  • 2020-12-04 13:41

    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>
    
    0 讨论(0)
  • 2020-12-04 13:44

    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"/>
    
    0 讨论(0)
  • 2020-12-04 13:45

    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.

    0 讨论(0)
  • 2020-12-04 13:45

    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.

    0 讨论(0)
提交回复
热议问题