public override void OnWindowFocusChanged(bool hasFocus) { base.OnWindowFocusChanged(hasFocus); // the service helper is responsible for running actions that depend on the main activity. if the main activity // is not showing, the service helper starts the main activity and then runs requested actions. there is a race // condition between actions that wish to show a dialog (e.g., starting speech recognition) and the display of // the activity. in order to ensure that the activity is showing before any actions are run, we override this // focus changed event and let the service helper know when the activity is focused and when it is not. this // way, any actions that the service helper runs will certainly be run after the main activity is running // and focused. AndroidSensusServiceHelper serviceHelper = SensusServiceHelper.Get() as AndroidSensusServiceHelper; if (serviceHelper != null) { if (hasFocus) { serviceHelper.SetFocusedMainActivity(this); } else { serviceHelper.SetFocusedMainActivity(null); } } }