-1

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.

masmic
  • 3,526
  • 11
  • 52
  • 105
  • 1
    What is your "context" variable in this class? is the Activity itself? On the other hand, if you don't want to go back to this activity "finish();" it just before launching the new Activity. – Heisenberg May 12 '14 at 11:21
  • @Heisenberg context variable refers to the activity itself. – masmic May 12 '14 at 12:15

1 Answers1

2

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;
            }

    }
Sreedhu Madhu
  • 2,480
  • 2
  • 30
  • 40