0

Just playing with Android Studio, and trying to add an Preference Activity, to one of the Google UI samples, but not getting very far. For a start the New->Activity->Settings Activity wizard does not appear to know about preference-headers tags, so fails to include an @overide for: isValidFragment(String) and onBuildHeaders(List target) in the code it generates. Anyway after hacking two example app's together and adding a isValidFragment() function e.g.

public class SettingsActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onBuildHeaders(List<Header> target) {
            loadHeadersFromResource(R.xml.pref_headers, target);
    }

    @Override
    protected boolean isValidFragment(String fragmentName) {
        return SettingsActivity.class.getName().equals(fragmentName)
                || super.isValidFragment(fragmentName);
    }

...


public static class DataSyncPreferenceFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_data_sync);

        // Bind the summaries of EditText/List/Dialog/Ringtone preferences
        // to their values. When their values change, their summaries are
        // updated to reflect the new value, per the Android Design
        // guidelines.
        bindPreferenceSummaryToValue(findPreference("prefSyncToGoogle"));
        bindPreferenceSummaryToValue(findPreference("prefSyncToApple"));
        bindPreferenceSummaryToValue(findPreference("prefSyncToMyFitnessPal"));
    }
}

I still get the error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.slidingtabsbasic/com.example.android.slidingtabsbasic.SettingsActivity}: java.lang.RuntimeException: Subclasses of PreferenceActivity must override isValidFragment(String) to verify that the Fragment class is valid! com.example.android.slidingtabsbasic.SettingsActivity has not checked if fragment com.example.android.slidingtabsbasic.SettingsActivity$DataSyncPreferenceFragment is valid.

where the pref_headers.xml file is:

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

    <header
        android:fragment="com.example.android.slidingtabsbasic.SettingsHeaders$GeneralPreferenceFragment"
        android:title="@string/pref_header_general" />
    <header
        android:fragment="com.example.android.slidingtabsbasic.SettingsHeaders$NotificationPreferenceFragment"
        android:title="@string/pref_header_notifications" />
    <header
        android:fragment="com.example.android.slidingtabsbasic.SettingsHeaders$DataSyncPreferenceFragment"
        android:title="@string/pref_header_data_sync" />

</preference-headers>

A quick debug reveals:

Ok,

SettingsActivity.class.getName() = "com.example.android.slidingtabsbasic.SettingsActivity"

While:

fragmentName  = "com.example.android.slidingtabsbasic.SettingsActivity$DataSyncPreferenceFragment"

So the isValidFragment() call above obviously won't work, so is the only option to define a static array of fragment names in the function and do a lookup?

e.g.

private final String[] FRAGMENTS = {
        "com.example.android.slidingtabsbasic.SettingsActivity$GeneralPreferenceFragment",
        "com.example.android.slidingtabsbasic.SettingsActivity$NotificationPreferenceFragment",
        "com.example.android.slidingtabsbasic.SettingsActivity$DataSyncPreferenceFragment"
};

...

@Override
protected boolean isValidFragment(String fragmentName) {

    for (String FRAGMENT : FRAGMENTS) {
        if (FRAGMENT.equals(fragmentName)) {
            return true;
        }
    }
    return super.isValidFragment(fragmentName);
}
arober11
  • 1,969
  • 18
  • 31

1 Answers1

0

See isValidFragment Android API 19 and Is it possible to get all the subclasses of a class?

For security purposes, you must explicitly define them. You could consider parsing your xml fragments.

Community
  • 1
  • 1
Nick
  • 2,735
  • 1
  • 29
  • 36