Android settings snippet

I am trying to add a settings snippet to my Android application. So, I added an 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 is called from the onOptionsItemSelected function in the main action:

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

So, now when I try to open an action from the menu, it forcibly 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)
+4
source share
2 answers

You are replacing a fragment that has never been added before ...

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

to try

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

But the correct code should be (pseudo code)

  • Find a fragment by id or tag
  • If the found fragment is null, create it.

You can find many examples around the web and stackoverflow .;)

UPDATE , .

. , , .

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

, , - ...

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

, . , .:)

+3

. MainActivity SettingsActivity. , , , ; .

:

<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>
+1

All Articles