5

I'm trying to add a settings fragment to my android-app. So, I added a xml-file and this activity:

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();
}

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);
    }
}
}

It's being called from the onOptionsItemSelected function in the main activity:

if (id == R.id.action_settings) {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
        return true;
    }

So, now, when I try to open the activity from the menu, it force-closes and I get this error in the console:

Force-removing child win Window{4190a9a8 u0 PopupWindow:418fc208} from container         Window{418dd448 u0 org.name.app/org.name.app.MainActivity}
Failed looking up window
java.lang.IllegalArgumentException: Requested window android.os.BinderProxy@423b1fc0 does not exist
        at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:7934)
        at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:7925)
        at com.android.server.wm.WindowState$DeathRecipient.binderDied(WindowState.java:1047)
        at android.os.BinderProxy.sendDeathNotice(Binder.java:493)
        at dalvik.system.NativeStart.run(Native Method)

2 Answers 2

4

You are replacing a fragment that never added before…

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

try

            .add(android.R.id.content, new SettingsFragment())

But the right code should be (pseudo code)

  1. Find fragment by id or tag
  2. If found fragment is null, then create one.

You can find plenty of examples around the net and stackoverflow. ;)

UPDATE Ok, here's some sample code for you to use as a starting point.

Assumptions: You use the same activity for more than one fragment, so you need to either restore an existing or create a new one.

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment newFragment = fm.findFragmentByTag("Some_Tag");
if (newFragment == null) {
     newFragment = SomeFragment.newInstance(); //create a new frag
}
// Find the old one to know if we have to replace or simply add to this container
Fragment oldFragment = fm.findFragmentById(R.id.content_container);
if (oldFragment != null) {
    ft.replace(R.id.content_container, newFragment, "Some_Tag");
} else {
    ft.add(R.id.content_container, newFragment, "Some_Tag");
}
// optionally use a nice transition
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// …and to the backstack if you wish…
ft.addToBackStack(null).commit();

If on the other hand you just want the simple version without anything fancy…

FragmentManager fm = getSupportFragmentManager();//if using support lib
Fragment fragment = fm.findFragmentById(R.id.your_container);
if (fragment == null) {
   fragment = YourFragment.newInstance();
   fm.beginTransaction()
   .add(R.id.your_container, fragment, "some_tag_if_you_wish_to_use_find_by_tag_later")
   .commit();
}

This will only add the fragment if the container doesn't have it. Otherwise nothing remains to be done because the container already has your fragment. :)

1
  • I replaced the replace function by the add function, but it doesn't work either. Do I need to find the old fragment and replace it by the new one, or what am I doing wrong? Dec 23, 2013 at 18:40
1

The error message indicates some problem with the activity hierarchy. Try making the MainActivity the parent of the SettingsActivity. I'm using the same code you have, except for the parent relationship I mentioned; and it works for me.

You can set the parent relatioship like this:

<activity
            android:name="some.package.SettingsActivity"
            android:label="@string/activity_name_settings"
            android:parentActivityName="some.package.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="some.package.MainActivity" />
</activity>

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.