Search code examples
androidandroid-fragmentspreferenceactivityandroid-3.0-honeycomb

Start activity from preference-headers


I'm implementing n-layer PreferenceActivities 1st layer PreferenceActivity is loaded from preference-headers.

First header creates fragment of settings which is a PreferenceFragment. Second is a browser activity (2nd is an example from developer.android.com) which opens specified Url. The third one I want to be a next level of PreferenceAtivity that also will be loaded from preference-headers.

First two work fine but 3rd is crashing an app with the exception:

"android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=com.mycompany.myapp.ui.MyPreferenceActivity} "

MyPreferenceActivity declared in the manifest file. Probably declaration of activity in main_headers.xml is wrong, but I didn't find in references any tip how to do it correct. Tried several variations, they didn't work.

Example I used: http://developer.android.com/reference/android/preference/PreferenceActivity.html

Any thoughts why it doesn't work for me or how the next PreferenceActivity can be called? Basically I just need to start an activity from header, that should be really simple but I'm missing something.

public class MySettings extends PreferenceActivity 
{
    @Override
    public void onBuildHeaders(List<Header> target) 
    {
        loadHeadersFromResource(R.xml.main_headers, target);
    }
}

main_headers.xml:

<?xml version="1.0" encoding="utf-8"?>
<preference-headers
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <header android:title="Custom Settings"
        android:fragment="com.mycompany.myapp.ui.SettingsFragment" />

    <header android:title="Intent"
        android:summary="Launches an Intent.">
        <intent android:action="android.intent.action.VIEW"
                android:data="http://www.android.com" />
    </header>

    <header android:title="Another Preference Activity">
        <intent android:action="android.intent.action.VIEW"
            android:data="com.mycompany.myapp.ui.MyPreferenceActivity" />
    </header>
</preference-headers>

Solution

  • If you want to start an explicit Activity from your third preference then do this:

    <intent android:targetPackage="com.mycompany.myapp"
            android:targetClass="com.mycompany.myapp.ui.MyPreferenceActivity" />