Reputation: 1193
When onCreateView called second time it adds another instance of MyFragment. For example when this Fragment comes back from back stack, it shows two instances of MyFragment. Why? How should I prevent it?
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.announcement, null);
FragmentTransaction fragmentTransaction = getChildFragmentManager()
.beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer, new MyFragment());
fragmentTransaction.commit();
return view;
}
Upvotes: 0
Views: 1027
Reputation: 1193
FragmentTransaction should done in onCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentTransaction fragmentTransaction = getChildFragmentManager()
.beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer, new MyFragment());
fragmentTransaction.commit();
}
thanks to Atrix1987
Upvotes: 2