2

I'm following the preference fragment solution in this page https://developer.android.com/guide/topics/ui/settings.html

Here is the code used:

public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();
}
}

This solution works but before I did this I tried something different.

At first, I tried opening the Settings without using SettingsActivity. I wanted to switch the my main fragment in my main activity with Settings Fragment as such (this code is inside my MainActivity file):

 public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        fragmentTransaction.replace(R.id.container, new SettingsFragment());
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

        return true;
    }

    return super.onOptionsItemSelected(item);
}

This R.id.container is the FrameLayout in my activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >

</FrameLayout>

This approach doesn't seem to work, why? The Settings fragment goes over the current fragment since the current fragment is not replaced.

What would be the performance penalties if I choose to make a new activity with the menu fragment instead?

Thank you for your help.

1 Answer 1

2

Your code looks fine but you can try this

FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();
    fragmentTransaction.replace(android.R.id.content, new SettingsFragment());
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

Where android.R.id.content gives you the root element of a view, without having to know its actual name, type or ID.

1
  • I tried that solution but my settings fragment still overlaps with my main fragment.
    – MrBuggy
    May 10, 2016 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.