lostintranslation
lostintranslation

Reputation: 24563

SharedPreferences sub PreferenceScreen with custom fragment class

I have a PreferenceScreen with a sub PreferenceScreen:

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

    <PreferenceCategory android:title="SOUND">
        <PreferenceScreen android:title="Sound Options" 
            android:fragment="com.test.SoundPreferenceFragment">

        </PreferenceScreen>
    </PreferenceCategory>
...

The sub preference is a custom implementation of a PreferenceFragment

public class SoundPreferencesFragment extends PreferenceFragment implements
        OnSharedPreferenceChangeListener {
...

I am wondering how I am supposed to start the custom fragment. In my main activity I do this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content, settingsPreference).commit();
}

Which works great to start my main settings preference screen.

I then override:

    @Override
    public boolean onPreferenceStartFragment(PreferenceFragment fragment, Preference preference) {

        getFragmentManager().beginTransaction().replace(android.R.id.content, new SoundPreferenceFragment()).commit();

       return true;
    }

That seems to work but I have a few questions.

  1. Is that the right way?
  2. When I go into the Sounds preference and hit the back button it takes me all the way out of settings. Is that how it should work?
  3. I think I can also tell the sub preference to start a new intent, i.e. create a new SoundPreferencesActivity. Is that a more 'correct' way.

EDIT:

I also tried not to extend 'onPreferenceStartFragment' in my main activity hoping that android would take care of starting the new preference correctly' It does seem to start the new PreferenceFragment but it overlays the UI right on top of the main preferences UI.

Upvotes: 0

Views: 640

Answers (1)

Libin
Libin

Reputation: 17085

You have to create a new activity and attach the SoundPreferencesFragment . when you select the item in main setting activity , start the new activity

Upvotes: 1

Related Questions