My app has an Activity A, which contains a fragment B. In B, user clicks a button and a child fragment C is created in B. Now user backgrounds the app by pressing home button. After awhile my app is killed and restarted by Android OS.
Then user launches the app again. Now Activity A is started and both fragment B and its child fragment C are recreated automatically. My question, how to prevent child fragment C gets recreated automatically? I try to remove fragment C from B in B's onAttachFragment method during restarting, but it didn't work.
@Override
public void onAttachFragment(Fragment f) {
super.onAttachFragment(f);
if (isRestart) {
getChildFragmentManager().beginTransaction()
.remove(f)
.commit();
isRestart = false;
}
}