Permalink
Browse files

Always use FragmentTransaction.replace() instead of add().

This removes any existing fragments with the same ID.
At least on Andoid Lollipop, I have sometimes seen two set of fragments,
doubling the actoin icons and menu items.
This was maybe happening when we checked for the existing fragment too
soon after adding it, then adding another one when we didn't find it.
This suggests an inefficiency, but until I find that, this is safer
anyway.
  • Loading branch information...
murraycu committed Nov 24, 2014
1 parent dd09d98 commit 7dd2af782464760c6541f694e1ba123180784459
@@ -251,7 +251,7 @@ protected void onCreate(Bundle savedInstanceState) {
fragment = new ClassifyFragment();
fragment.setArguments(arguments);
fragmentManager.beginTransaction()
.add(R.id.container, fragment)
.replace(R.id.container, fragment)
.commit();
} else {
Log.info("ClassifyActivity.onCreate(): The ClassifyFragment already existed.");
@@ -219,7 +219,7 @@ private void addOrUpdateChildFragments() {
fragmentSubject = new SubjectFragment();
fragmentSubject.setArguments(arguments);
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.child_fragment_subject, fragmentSubject).commit();
transaction.replace(R.id.child_fragment_subject, fragmentSubject).commit();
} else {
//TODO: Is there some more standard method to do this,
//to trigger the Fragments' onCreate()?
@@ -234,7 +234,7 @@ private void addOrUpdateChildFragments() {
fragmentQuestion = new QuestionFragment();
fragmentQuestion.setArguments(arguments);
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.child_fragment_question, fragmentQuestion).commit();
transaction.replace(R.id.child_fragment_question, fragmentQuestion).commit();
} else {
//TODO: Is there some more standard method to do this,
//to trigger the Fragments' onCreate()?
@@ -54,7 +54,7 @@ protected void onCreate(Bundle savedInstanceState) {
fragment = new ExampleViewerFragment();
fragment.setArguments(arguments);
fragmentManager.beginTransaction()
.add(R.id.container, fragment)
.replace(R.id.container, fragment)
.commit();
} else {
Log.info("ExampleViewerActivity.onCreate(): The ExampleViewerFragment already existed.");
@@ -74,7 +74,7 @@ protected void onCreate(Bundle savedInstanceState) {
fragment = new QuestionHelpFragment();
fragment.setArguments(arguments);
fragmentManager.beginTransaction()
.add(R.id.container, fragment)
.replace(R.id.container, fragment)
.commit();
} else {
Log.info("QuestionHelpActivity.onCreate(): The QuestionHelpFragment already existed.");
@@ -78,7 +78,7 @@ protected void onCreate(Bundle savedInstanceState) {
fragment = new SubjectViewerFragment();
fragment.setArguments(arguments);
fragmentManager.beginTransaction()
.add(R.id.container, fragment)
.replace(R.id.container, fragment)
.commit();
} else {
Log.info("SubjectViewerActivity.onCreate(): The SubjectViewerFragment already existed.");
@@ -122,7 +122,7 @@ private void addOrUpdateChildFragments() {
fragmentSubject = new SubjectFragment();
fragmentSubject.setArguments(arguments);
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.child_fragment_subject, fragmentSubject).commit();
transaction.replace(R.id.child_fragment_subject, fragmentSubject).commit();
} else {
//TODO: Is there some more standard method to do this,
//to trigger the Fragments' onCreate()?
@@ -137,7 +137,7 @@ private void addOrUpdateChildFragments() {
fragmentQuestion = new SubjectExtrasFragment();
fragmentQuestion.setArguments(arguments);
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.child_fragment_subject_extras, fragmentQuestion).commit();
transaction.replace(R.id.child_fragment_subject_extras, fragmentQuestion).commit();
} else {
//TODO: Is there some more standard method to do this,
//to trigger the Fragments' onCreate()?

0 comments on commit 7dd2af7

Please sign in to comment.