Pregunta

INTRODUCTION

I have an activity that processes some functions. Inside this activity, the main process is one thread that makes the processing of these functions.When the processing is done, it should call to another activity to start another diferent process.

This is my thread inside the main activity:

CODE

private static void DetectionThread (byte[] data, int width, int height, final Context context) {

        mData = data;
        mWidth = width;
        mHeight = height;

        mThread = new Thread() {

            @Override
            public void run() {

                try {
                    //MAKES THE PROCESSING
                    //If it's right, continues to next code...

                        MotionDetectionActivity.gameStarted = true;
                        gameLaunched = true;
                        return;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    processing.set(false);

                    /*HERE MUST INIT THE ACTIVITY WITH INTENT*/
                    if (MotionDetectionActivity.gameStarted == true && gameLaunched == true) {
                        gameLaunched = false;

                        Intent gameIntent = new Intent(context, GameActivity.class);
                        context.startActivity(gameIntent);
                    }
                    processing.set(false);
                }
            }
        };
        if (MotionDetectionActivity.gameStarted == false) {
            mThread.start();
        }
}

QUESTION

Well, the thing is that i'm not getting the desired result. When initializing the GameActivity, it is not showing this activity's layout, and there are some functionalities that are not initialized, f.e. I do this to initialize the TTS:

private static TextToSpeech tts;

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

    tts = new TextToSpeech(this, this);

//Iniside main method
tts.speak("Initializing...", TextToSpeech.QUEUE_ADD, null);

The thing is that it doesn't talk.

¿Fue útil?

Solución

Use AsyncTask instead of Thread, and call the another activity in the onPostExecute method

    public class MyAsync extends AsyncTask<Void, Void, Void>
        {
            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
//start the next activity here
            }

            @Override
            protected Void doInBackground(Void... params) {
//your task goes here
                return null;
            }

    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top