Home » Android » android – Start activity from preference-headers

android – Start activity from preference-headers

Posted by: admin June 15, 2020 Leave a comment

Questions:

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>
How to&Answers:

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" />

Answer:

 <header android:title="Intent"
    android:summary="Launches an Intent.">
<intent android:targetPackage="com.mycompany.myapp"
        android:targetClass="com.mycompany.myapp.ui.MyPreferenceActivity" /></header>

and add the below code in mainfest file

<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>

Answer:

if you are creating preferences using java ,then you can achieve the same result by,

PreferenceScreen editPhoneNumbersScreen = getPreferenceManager().createPreferenceScreen(mContext);
    editPhoneNumbersScreen.setTitle(getResources().getString(R.string.change_add_emergency_numbers));
    editPhoneNumbersScreen.setSummary(getResources().getString(R.string.summary_add_no));


 Intent i = new Intent(getApplicationContext(),TargetActivity.class);
i.putExtra("change numbers", true);
        editPhoneNumbersScreen.setIntent(i);