title: Close reward video ad in Android
body: <p>I am designing an app that includes a rewarded video before giving the player another life. For user experience and engagement, I want to ensure that this rewarded video is closed after 30 seconds and the user is taken back to the app. We are using a client-side mediation solution for 4 of the largest video networks (Chartboost, Vungle, App Lovin, and Ad Colony).</p>
  
  <p>Many ad networks provide this close functionality, such as MoPub's <code>MoPubView.destroy()</code>. However, not all networks provide a programmatic method to close their ads.</p>
  
  <p>All the ad networks I've come across extend the <code>View</code> class to show their ads. This means that the ad itself must be in the app's <code>View</code> stack.</p>
  
  <p>Is there a way to cycle through <code>Views</code> and close the one that matches an ad? It seems this should be possible, since MoPub's SDK uses the following code:</p>
  
  <pre><code>public void destroy() {
      unregisterScreenStateBroadcastReceiver();
      removeAllViews();
      ....
  }
  </code></pre>
  
  <p>And <code>removeAllViews()</code> is a <a href="http://developer.android.com/reference/android/view/ViewGroup.html" rel="nofollow noreferrer"><code>ViewGroup</code></a> method composed of:</p>
  
  <pre><code>public void removeAllViews() {
      removeAllViewsInLayout();
      requestLayout();
      invalidate(true);
  }
  </code></pre>
  
  <p>I'm stumped with combining this <code>ViewGroup</code> code with a <code>View</code> iterator like in <a href="https://stackoverflow.com/questions/4872238/enumerate-iterate-all-views-in-activity">Enumerate/Iterate all Views in Activity?</a>. Any thoughts?</p>