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,259
  • 21
  • 28
  • 43
Student222
  • 2,871
  • 2
  • 16
  • 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,726
  • 3
  • 25
  • 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
  • 243
  • 3
  • 9