Search code examples
androidandroid-preferences

Android PreferencesActivity showing previous fragment


I created a Preferences screen by extending PreferencesActivity as per the example here.

The first screen appears okay but when I click to see the second screen I see them one on top of the other. So, taking advice from a number of other thread, I changed the background of the second fragment to black.

This worked in as much as I no longer see them both. But instead I see the FIRST one only except for the header.

The first screen looks like this:

enter image description here

and the second one looks like this:

enter image description here

Only the header line changed whereas the rest remained the same.

This is the code in my activity:

public class SettingsActivity  extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Load the preferences from an XML resource
    getFragmentManager().beginTransaction().replace(android.R.id.content, new PreferencesMain()).commit();


}

/**
 * This fragment contains a second-level set of preference that you
 * can get to by tapping an item in the first preferences fragment.
 */
public static class PreferencesMain extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    public void onStart() {
        super.onStart();
        View view = getView();
        view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
    }
}

/**
 * This fragment contains a second-level set of preference that you
 * can get to by tapping an item in the first preferences fragment.
 */
public static class PreferencesNotifications extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences_notifications);

    }

    @Override
    public void onStart() {
        super.onStart();
        View view = getView();
        view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
    }
}

@Override
protected boolean isValidFragment (String fragmentName) {
    return true;
}

}

preferences.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
    android:title="@string/settings_notifications">

    <!-- This PreferenceScreen tag sends the user to a new fragment of
         preferences.  If running in a large screen, they can be embedded
         inside of the overall preferences UI. -->
    <PreferenceScreen
        android:fragment="activities.SettingsActivity$PreferencesNotifications"
        android:title="@string/settings_notifications_managePushTitle"
        android:summary="@string/settings_notifications_managePushSummary">
        android:background="@android:color/black"
        <!-- Arbitrary key/value pairs can be included for fragment arguments -->
        <extra android:name="someKey" android:value="somePrefValue" />
    </PreferenceScreen>

</PreferenceCategory>

preferences_notifications.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory
    android:title="@string/settings_notifications_managePushHeader">

<CheckBoxPreference
    android:key="checkbox_preference"
    android:title="@string/settings_notifications_managePushEntry1Title"
    android:summary="@string/settings_notifications_managePushEntry1Summary" />
</PreferenceCategory>


Solution

  • Add a header.xml to manage settings, and use onBuildHeaders method.

    Resource headers.xml:

    <preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
        <header android:title="Digle"
            android:fragment="activities.SettingsActivity$PreferencesMain"/>
    </preference-headers>
    

    Class SettingsActivity:

    public class SettingsActivity extends PreferenceActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Remove all code in here
        }
    
        @Override
        public void onBuildHeaders(List<Header> target) {
            super.onBuildHeaders(target);
            loadHeadersFromResource(R.xml.headers, target);
        }
    
        ...
    }
    

    EDIT: An alternative could be use subscreens. You have to place the group of Preference objects inside a PreferenceScreen.

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
                      android:key="Settings"
                      android:title="@string/app_name">
        <PreferenceCategory
            android:title="@string/settings_notifications">
    
            <PreferenceScreen
                android:background="@android:color/black"
                android:summary="@string/settings_notifications_managePushSummary"
                android:title="@string/settings_notifications_managePushTitle">
    
                <PreferenceCategory
                    android:title="@string/settings_notifications_managePushHeader">
    
                    <CheckBoxPreference
                        android:key="checkbox_preference"
                        android:summary="@string/settings_notifications_managePushEntry1Summary"
                        android:title="@string/settings_notifications_managePushEntry1Title"/>
    
                </PreferenceCategory>
    
            </PreferenceScreen>
    
        </PreferenceCategory>
    
    </PreferenceScreen>