title: When are views attached and detached?
body: <p>This question is <em>not</em> about how to detect if a view is attached or detached.</p>
  
  <p>In general, when is a view attached or detached? Is there a lifecycle diagram for this?</p>
  
  <p>To clarify, I'm looking for answers to what happens when: Activity sent to background, opaque view placed on top, visibility set to <code>GONE</code>, view inflated, parent detached, etc. This is not an exhaustive list - I just want to understand how attaching and detaching of views works, at a fundamental level.</p>
  
  <p>Update with more examples of what I'm trying to get at:</p>
  
  <p>What about fragments vs. activities?<br>
  What about nested views - in what order are views attached/detached (parent->child or child->parent)?<br>
  Are views measured before they are attached or after?<br>
  What about using addView() to a ViewGroup manually?  </p>
  
  <p>Edit: Summary:  </p>
  
  <ul>
  <li>For Activities, views are attached in <code>setContentView()</code>. Views are detached in <code>onDestroy()</code> or when <code>setContentView()</code> is called with a different view.</li>
  <li>For Fragments, views are attached <a href="http://developer.android.com/reference/android/app/Fragment.html#onViewCreated%28android.view.View,%20android.os.Bundle%29">after <code>onViewCreated()</code> finishes</a>, and are detached after <code>onDestroyView()</code> finishes.</li>
  <li>For ViewGroups, views are attached in <code>addView()</code> and detached in <code>removeView()</code></li>
  <li><code>setVisibility()</code> does not affect the attached state of a view</li>
  </ul>