0

I have an Activity with several buttons and text elements. These elements reside in the Activity itself, not inside a fragment.

However, I want to be able to replace all this content with a PreferencesScreen fragment. Is this possible at all without putting the rest of the contents in my Activity that I want removed into a fragment and then call the .replace() function?

Currently I do

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

but this simply puts the ProfileFragment on top of my Activity. It overlays it. So I see and can interact with the ProfileFragment, but I also see the rest of the contents of the Acitvity underneath.

Again: I want to be able to replace all the content of my Activity with a PreferencesScreen fragment. Is this possible at all without putting the rest of the contents in my Activity that I want removed into a fragment and then call the .replace() function?

  • yes you answered yourself, the replace is used to replace fragments not views – headsvk Feb 26 '16 at 19:01
  • If the problem is that you don't want to see the activity underneath, you could just add a background color to your fragment's view. – ootinii Feb 26 '16 at 19:02
  • @headsvk But can I use something else than replace() to achieve what I want? –  Feb 26 '16 at 19:02
  • probably not, u can simulate it by invalidating your current layout and calling setContentView() – chaitanya Feb 26 '16 at 19:03
  • @ootinii good point, but I assume that's pretty bad standard, no? –  Feb 26 '16 at 19:03
  • well you don't have a layout in preference fragment, it's created on runtime by the framework for the supplied preference file – headsvk Feb 26 '16 at 19:05
  • 1
    If it achieves the visual experience you want, I wouldn't say it's necessarily bad. – ootinii Feb 26 '16 at 19:09

1 Answers1

0

If you want to just hide the activity content, here's how you can set the background color of your Preference Fragment (tested with PreferenceFragmentCompat).

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    view.setBackgroundColor(Color.WHITE);
}

Note that settings are typically opened in a new activity (as in Google apps).

If you're using PreferenceFragmentCompat, these SO answers may help you.

Styling issues: https://stackoverflow.com/a/32108439/2627680

Opening inner preference screens: https://stackoverflow.com/a/32540395/2627680

Community
  • 1
  • 1
headsvk
  • 2,726
  • 1
  • 19
  • 23
  • It seems I have misunderstood the documentation when it comes to PreferenceFragments. I think you are very much correct in saying that settings typically are opened in a new activity! Thanks a bunch! –  Feb 26 '16 at 19:19
  • The preference documentation is not good especially for the v7 and v14 support preference libraries. If you're using those than only googling can help you. – headsvk Feb 26 '16 at 19:45