Search code examples
androidtext-to-speech

Text to Speech call onPause After onCreate


I'm using the Text to Speech engine in Android. I'm having problems with the way it calls onPause straight after onCreate. So first of all is there a way to stop this?

I have a game which has a MENU activity and a GAME activity; when the user hits the back button to go to the MENU activity from the GAME activity onPause is called, I'm using onPause here for some useful stuff like stopping a countdown and stopping the TTS engine.

My problem arises when onPause is called after onCreate and it tries to stop a counter that isn't counting and a TTS engine which isn't initialised. I've managed to sort the countdown by starting a spurious countdown on onCreate for the onPause to stop and I can probably do the same thing for the TTS engine but it's obviously not a great thing to be doing.

Can anyone think of another, cleaner way around this?

Thanks

@Override
public void onPause() {
    super.onPause();
    timerCount.cancel();
    mTts.stop();
    mTts.shutdown();
}

Solution

  • If anyone has the same problem in the future, the way to get around TTS calling onPause after onCreate and the problems that causes is to either use the onBackPressed command or (as Pedro mentioned) setting a flag which is false on onCreate and setting onPause to set it true.