Attila Nyers
Attila Nyers

Reputation: 1193

Why adds OnCreateView the child fragments multiple times?

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

Answers (1)

Attila Nyers
Attila Nyers

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

Related Questions