15

The application's default package is "example.app".

and the target activity's package is "example.app.abc".

Calling startActivity() for "example.app.abc.TheActivity" in java code just works,

but specifying it on preference.xml doesn't work:

<PreferenceScreen android:key="key"
    android:title="@string/title"
>
    <intent android:action="android.intent.action.MAIN"
        android:targetPackage="example.app.abc"
        android:targetClass="TheActivity"
/>
</PreferenceScreen>

I tried android:targetClass="example.app.abc.TheActivity" but it doesn't work too.

Is it impossible to start an activity of non-default package, in preference?

shkim
  • 1,173
  • 3
  • 16
  • 24
  • I don know if its possible to start a non default package. Whydont u try again with a "." in front of the targetClass. like android:targetClass=".TheActivity" – Varun Dec 05 '10 at 18:00
  • 1
    Where do you see the nested `` feature documented? – CommonsWare Dec 05 '10 at 18:32
  • It should not be documented, as it's unsupported internal API. The application might be rejected by using it. – Pentium10 Dec 05 '10 at 20:03
  • I didn't know it's undocumented API. My friend's app actually uses the above technique and runs well with the default package's activity. – shkim Dec 06 '10 at 04:51
  • 2
    @Pentium10: It is not undocumented, as you can find it in the API Demos project as an example. – Skrud Jan 26 '11 at 19:07
  • same issue here.. ever found an answer? – Hermit Apr 12 '12 at 11:34
  • 1
    @CommonsWare It is mentioned [here](http://developer.android.com/guide/topics/ui/settings.html#Intents) – darrenp Mar 22 '13 at 14:09

2 Answers2

30

I just ran into the same problem when trying to use a custom preference screen from a library project for the AccountManager account settings. No matter how I tried to tweak the targetPackage and targetClass attributes, it would throw an exception (except, since it's an account, it crashes the phone).

I think we'll just have to assume this is an Android limitation. It's clumsy, but all you really need to do is declare a wrapper class for the activity within your application's namespace:

public class MyPreferences extends ActualPreferences {
}

Declare it in your AndroidManifest.xml

<activity android:name=".MyPreferences"/>

Then you can specify the class in your intent

<intent android:targetPackage="com.my.package"
        android:targetClass="com.my.package.MyPreferences" />

By the way, the syntax is extremely fussy, at least for account preferences. All these variations fail:

<!-- fails --> <intent android:targetClass="com.my.package.MyPreferences" />
<!-- fails --> <intent android:targetClass="MyPreferences" 
                       android:targetPackage="com.my.package"/>
<!-- fails --> <intent android:targetClass=".MyPreferences"
                       android:targetPackage="com.my.package"/>
<!-- fails --> <intent android:targetClass="settings.MyPreferences"
                       android:targetPackage="com.my.package"/>
<!-- fails --> <intent android:targetClass=".settings.MyPreferences"
                       android:targetPackage="com.my.package"/>
<!-- fails --> <intent android:targetClass="com.my.other.package.MyPreferences"
                       android:targetPackage="com.my.package"/>

The critical factor is apparently that the android:targetPackage attribute matches the application package. If you want, you can put the activity in a sub-package. This works:

<intent android:targetPackage="com.my.package"
        android:targetClass="com.my.package.settings.MyPreferences" />
Community
  • 1
  • 1
ehartwell
  • 1,667
  • 19
  • 18
1

as already said its not working with libraries. Do it programatically, something like this:

preference_my_pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                public boolean onPreferenceClick(Preference preference) {
                    Intent intent = new Intent(MyActivity.this, ActivityToStart.class);
                    startActivity(intent);
                    return true;
                }
            });
user1324936
  • 2,187
  • 4
  • 36
  • 49
  • you might wanna copy the extras from the original intent like `newIntent.putExtras(oldIntent)` – NikkyD Sep 04 '14 at 10:59