0

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!

Taras
  • 2,526
  • 3
  • 33
  • 63
  • This code is used to force `onCreateView`. `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 so keep unrelated code out. If you need to do everything brand new you might as well instantiate a new fragment. – Eugen Pechanec Apr 27 '15 at 15:34
  • @EugenPechanec Yes, it makes sense, thanks! This is definitely an answer. – Taras Apr 27 '15 at 15:36

1 Answers1

1

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.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124