4

I have a library project that I use to build two applications, a public one and a private one for personal usage with few more settings useless to most people. In this library project I define a PreferenceActivity (for API < Honeycomb) and some PreferenceFragments (for API >= Honeycomb), with preference headers using this guide : http://developer.android.com/guide/topics/ui/settings.html#BackCompatHeaders

To support older devices, I define this XML file as shown in the guide :

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <Preference 
        android:title="@string/base_de_donnees"
        android:summary="@string/summary_pref_restaurer_bdd_sd">
        <intent
            android:targetPackage="com.me.app_lib"
            android:targetClass="com.me.app_lib.activities.preferences.SettingsActivity"
            android:action="com.me.app_lib.activities.preferences.SettingsActivity.ACTION_PREF_BDD" />
    </Preference>

   <Preference 
       android:title="@string/saisie"
       android:summary="@string/summary_pref_saisie">
       <intent
           android:targetPackage="com.me.app_lib"
           android:targetClass="com.me.app_lib.activities.preferences.SettingsActivity"
           android:action="com.me.app_lib.activities.preferences.SettingsActivity.ACTION_PREF_SASISIE" />

   </Preference>
</PreferenceScreen>

Where com.me.app_lib is the package of the library project, in which the SettingsActivity is defined. However, I get a crash when the intent is called from one of the child projects, as the targetPackage does not match that of the child project. If I change com.me.app_lib to com.me.app_public (package of one of the child projects) it works, but as I have two child projects, this is not an option.

Do I have to copy this file to each of the child projects and change only the targetPackage line, or is there a better option ?

personne3000
  • 1,780
  • 3
  • 16
  • 27
  • have a look at this question: http://stackoverflow.com/questions/4360100/activitynotfoundexception-when-different-packages-targetclass-in-preferencescre – user1324936 Dec 19 '13 at 14:40

1 Answers1

4

What I did in the end is define com.me.app_lib as a resource string. The android:targetPackage attribute becomes:

android:targetPackage="@string/package_activity_preferences"

Then I can define a different value in both child projects for this string. This is not as clean as I would like it to be, but at least I do not need to copy-paste the same file in both child projects. The targetClass is part of my library project, so I do not need to change anything about it; only the targetPackage attribute was a problem.

personne3000
  • 1,780
  • 3
  • 16
  • 27