Taras
Taras

Reputation: 2576

Re-create fragment properly

I want to re-create Fragment. After I did something like

fragmentTransaction.detach(someFragment);
fragmentTransaction.attach(someFragment);
fragmentTransaction.commit();

onAttach is not being called but only onCreateView, which means that fragment was not-really re-attached.

Please advise how to make full detach/attach cycle. Thank you!

Upvotes: 0

Views: 47

Answers (1)

Eugen Pechanec
Eugen Pechanec

Reputation: 38223

The original code is used to force onCreateView (which may be useful at times).

onAttach and onDetach are called when the activity changes, so keep unrelated code out of these methods.

onCreate is called only when the fragment is created (which is once for retained fragment, otherwise once for each configuration change) so keep unrelated code out.

If you need to do everything brand new you might as well instantiate a new fragment and replace the original one.

Upvotes: 1

Related Questions