문제

I start an activity from another activity with an intent. This second activity doesn't have any UI, it just initializes some variables and executes automatically a method that makes some processing.

First I start this method calling it from onCreate, but inside the method I use some functionalities that must implement some interfaces, so, I think that these methods are being executed before they have had time to initialize.

So my question is about how to start automatically a method when I start a new activity, but giving to it some time before starting, to let the implemented interfaces initialize.

UPDATE--

public class GameActivity extends Activity implements TextToSpeech.OnInitListener {

    private static TextToSpeech tts;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);

        tts = new TextToSpeech(this, this);
        //...
        SpeechWhenMotion();
    }


    public void SpeechWhenMotion() {
        //...
        tts.speak("Inicializando...", TextToSpeech.QUEUE_ADD, null);
        //...
    }

It doesn't speak because it does not have time to call onInit method from onInitListener.

도움이 되었습니까?

해결책

You should move your call to SpeechWhenMotion() from onCreate() to the onInit() method from TextToSpeech.OnInitListener. The whole point of the TextToSpeech.OnInitListener is that you get the callback to onInit() when the initialisation is complete.

You are correct that calling it in onCreate() is too early - you must wait for onInit().

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top