Sander van't Veer Sander van't Veer - 1 month ago 13
Android Question

PreferenceFragment is shown transparently

I'm trying to show a

PreferenceFragment
after I select the Preferences option in my ActionBar.
However, after replacing current content with the
PreferenceFragment
you can see the old content below it. As in, you can see straight through the preferences.

Am I missing something here? I used an example from a book I own, which didn't use any layout files for the preferences. Do you need those?

Used code:

Actionbar menu

private boolean MenuChoice(MenuItem item) {
switch (item.getItemId()) {
case 0:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
ReaderPreferences prefs = new ReaderPreferences();
fragmentTransaction.replace(android.R.id.content, prefs);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
return true;

}
return false;
}


PreferenceReader

public class ReaderPreferences extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// --load the preferences from an XML file---
addPreferencesFromResource(R.xml.preference);
}
}


Actual result:

enter image description here

As you can see, you look straight through my preferences.
What did I do wrong?

Answer

Eventually fixed it with a very easy fix.

I simply called the PreferenceFragment in a new Intent, and it worked perfectly.

For anyone with the same problem:

Prefs.java

public class ReaderPreferences extends PreferenceActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // --load the preferences from an XML file---
            addPreferencesFromResource(R.xml.preference);
        }

In my main screen when pressing a button:

Intent i = new Intent(this, Prefs.class);
            startActivity(i);

That's all. After setting the preferences simply press the back button and you're done.

Comments