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();
}
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");
}
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null).commit();
, , - ...
FragmentManager fm = getSupportFragmentManager();
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();
}
, . , .:)