title: Communication between nested fragments in Android
body: <p>I recently learned <a href="https://stackoverflow.com/a/39467807/3681880">how to make nested fragments in Android</a>. I don't know how communication is supposed to happen, though. </p>
  
  <p><a href="https://i.stack.imgur.com/l7SzK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/l7SzK.png" alt="enter image description here"></a></p>
  
  <p>From reading the <a href="https://developer.android.com/training/basics/fragments/communicating.html" rel="noreferrer">fragment communication documentation</a> I know that</p>
  
  <blockquote>
    <p>All Fragment-to-Fragment communication is done through the associated
    Activity. Two Fragments should never communicate directly.</p>
  </blockquote>
  
  <p>This makes sense for sibling fragments within an activity, but it doesn't make as much sense for parent-child fragment communication. Do I need to go all the way up to the Activity just for the Child Fragment to talk to the Parent Fragment? If the answer is a simple "yes" then I can do that. If it is a "no", then what would the code design look like? </p>
  
  <p>I see in the <a href="https://developer.android.com/about/versions/android-4.2.html#NestedFragments" rel="noreferrer">Nested Fragment documentation</a> that one can use <a href="https://developer.android.com/reference/android/app/Fragment.html#getParentFragment()" rel="noreferrer"><code>getParentFragment()</code></a> to get a reference to the parent fragment. So does that mean that the child should directly communicate with the parent? That seems opposite from what is encouraged with a normal fragment communicating with the parent activity.</p>