Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a specific PreferenceFragment from a PreferenceActivity?

I have a PreferenceActivity with several fragments:

R.xml.preferences: (shortened for better readability):

<?xml version="1.0" encoding="utf-8"?>
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
    <header android:fragment="fragments.Fragment1" android:id="@+id/fragment1" [...] />
    <header android:fragment="fragments.Fragment2" android:id="@+id/fragment2" [...] />
    [...]
</preference-headers>

SettingsActivity:

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

This will show a list entry with fragments.Fragment1, fragments.Fragment2, ... if SettingsActivity is started.

But now I want to pass a Bundle such that a specific PreferenceFragment is opened when starting the activity:

so I added this to SettingsActivity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null && savedInstanceState.getBoolean("shortcut")) {
        // directly jump to fragments.Fragment1
    }
}

I tried to load the fragment via getFragmentManager().findFragmentById(R.id.fragment1), but this returns null. But even if I had the correct instance, I would not know how to invoke it. Also, calling loadHeadersFromResource(R.xml.preferences_fragment1, target); does not work - This will throw a RuntimeException "XML document must start with tag; foundPreferenceScreen at Binary XML file". I have no ideas left and also a search on SO and Google did not return any relevant results.

So my question is: Is it possible to directly load a PreferenceFragment (e.g. fragments.Fragment1) from the Activitiy's onCreate method? If so, how?

like image 231
Michael Osl Avatar asked Dec 02 '12 18:12

Michael Osl


People also ask

How do I use preference fragments?

To inflate from XML, use the addPreferencesFromResource(int) . The root element should be a PreferenceScreen . Subsequent elements can point to actual Preference subclasses. As mentioned above, subsequent PreferenceScreen in the hierarchy will result in the screen break.

How do I set custom preferences on Android?

It's still possible to customise the appearance of a Preference item though. In your XML you have to declare the root element as android:id="@android:id/widget_frame , and then declare TextView as android:title and android:summary . You can then declare other elements you want to appear in the layout.

What is preference screen?

Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings. Represents a top-level Preference that is the root of a Preference hierarchy. A PreferenceActivity points to an instance of this class to show the preferences.


1 Answers

According to: http://developer.android.com/reference/android/preference/PreferenceActivity.html#EXTRA_SHOW_FRAGMENT

public static final String EXTRA_SHOW_FRAGMENT

Added in API level 11 When starting this activity, the invoking Intent can contain this extra string to specify which fragment should be initially displayed.

Constant Value: ":android:show_fragment"

intent = new Intent( this, SettingsActivity.class );
intent.putExtra( PreferenceActivity.EXTRA_SHOW_FRAGMENT, Fragment1.class.getName() );
intent.putExtra( PreferenceActivity.EXTRA_NO_HEADERS, true );
like image 67
Waza_Be Avatar answered Oct 25 '22 16:10

Waza_Be