title: CastCompanionLibrary detecting presence of Chromecast devices on network
body: <p>When starting the activity that has a MediaRouteButton implemented according to the CastCompanionLibrary docs, the button does not display even when there are Chromecast devices in the network. (Note the MediaRouteButton here is not in the ActionBar; the activity we're talking about here actually has no ActionBar, but it extends ActionBarActivity. Other activities in our application has the Cast Button implemented into the Action Bar according to the docs, which behaves correctly when there are Chromecast devices in the network.)</p>
  
  <p>We observed that onCastAvailabilityChanged (as well as onCastDeviceDetected) in the VideoCastConsumer was not being called. We traced the code and found that onRouteAdded in /cast/CastMediaRouterCallback.java wasn't being called when the activity starts.</p>
  
  <p>In our activity, we create the consumer in onResume</p>
  
  <pre><code>@Override
      protected void onResume() {
          mCastManager = VideoCastManager.getInstance();
          if (mCastManager != null) {
              mVideoCastConsumer = new VideoCastConsumerImpl() {
                  .....
                  @Override
                  public void onCastAvailabilityChanged(boolean castPresent) {
                      mMediaRouteButton.setVisibility(castPresent ? View.VISIBLE : View.GONE);
                  }
                  .....
              }
              mCastManager.incrementUiCounter();
              mCastManager.addVideoCastConsumer(mVideoCastConsumer);
          }
      …..
  </code></pre>
  
  <p>and do decrementUiCounter and remove videoCastConsumer from the manager in onPause, as the implementation docs state.</p>
  
  <p>However, we did observe that strangely the onCastAvailabilityChanged and onCastDeviceDetected callbacks <strong>always</strong> get triggered after we background and foreground the application. <strong>Any ideas why that happens? Is there a way to replicate this behaviour to happen on activity load or at least in onResume/onPause?</strong></p>
  
  <p>EDIT: Worth mentioning, we tried calling mCastManager.startCastDiscovery, but the callbacks weren't called until we backgrounded/foregrounded again.</p>