0

I read a few articles on avoiding circular references this morning. And I just realized that in Android, if an Activity contains a Fragment, that Fragment should also hold a reference to the containing Activity. Isn't it setting up a circular reference?

I feel this might be a silly question, but asking silly questions is a good way to learn, isn't it?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Student222
  • 3,021
  • 2
  • 19
  • 24

3 Answers3

0

The activity and Fragment has there own life cycles, there is no circular reference thing ,try to make your hands dirty with different Fragment and Activity Life cycles , you will come to know.

Haroon
  • 497
  • 4
  • 13
0

There's a useful discussion on the difference between Activity and Fragment which sheds some more light. The relationship between Activity and Fragment is such that a "Fragment" represents a behavior or a portion of user interface in an Activity. An Activity may contain 0 or multiple number of fragments (usually in two-pane views or Tablet UIs). You can read more on the subject from the official documentation here.

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
0

yes it's a circular reference but one that android manages by detaching the fragment from its host (therefore breaking the circular reference) before destroying, garbage collection can then occur.

hmac
  • 267
  • 3
  • 9