0

I have an imageButton that changes based on some variables, and I would like to let this process happen in the background.

I've tried putting it in a thread, but I don't understand AsyncTask that well quite yet.

public void initGameButtonOne() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            final ImageButton gameButton1 = getView().findViewById(R.id.multiPlayerGameButton1);
            final ImageButton gameButton2 = getView().findViewById(R.id.multiPlayerGameButton2);
            final ImageButton gameButton3 = getView().findViewById(R.id.multiPlayerGameButton3);
            final ImageButton gameButton4 = getView().findViewById(R.id.multiPlayerGameButton4);
            final ImageButton gameButton5 = getView().findViewById(R.id.multiPlayerGameButton5);
            final ImageButton gameButton6 = getView().findViewById(R.id.multiPlayerGameButton6);
            final ImageButton gameButton7 = getView().findViewById(R.id.multiPlayerGameButton7);
            final ImageButton gameButton8 = getView().findViewById(R.id.multiPlayerGameButton8);
            final ImageButton gameButton9 = getView().findViewById(R.id.multiPlayerGameButton9);
            gameButton1.getBackground().setAlpha(0);
            gameButton1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (player1Turn) {
                        gameButton1.setBackgroundResource(R.drawable.gameobutton);
                        gameButton1.getLayoutParams().height = 300;
                        gameButton1.getLayoutParams().width = 300;
                        gameButton1.getBackground().setAlpha(255);
                        gameButton1.setClickable(false);
                        player1Turn = !player1Turn;
                        player1ClickedButton1 = true;
                        if (!gameButton1.isClickable() &&
                                !gameButton2.isClickable() &&
                                !gameButton3.isClickable() &&
                                !gameButton4.isClickable() &&
                                !gameButton5.isClickable() &&
                                !gameButton6.isClickable() &&
                                !gameButton7.isClickable() &&
                                !gameButton8.isClickable() &&
                                !gameButton9.isClickable()) {
                            goToPlayerDrawScreen(getView());
                        }
                        checkPlayer1WinCondition();

                    } else {
                        gameButton1.setBackgroundResource(R.drawable.gamexbutton);
                        gameButton1.getLayoutParams().height = 220;
                        gameButton1.getLayoutParams().width = 220;
                        gameButton1.getBackground().setAlpha(255);
                        gameButton1.setClickable(false);
                        player1Turn = !player1Turn;
                        player2ClickedButton1 = true;
                        checkPlayer2WinCondition();
                    }
                }
            });
        }
    }).run();
}

The thing is that I just want the background resource and layout parameters to happen in the background, and I don't understand why this gives me such poor performance on the main thread when I've already put it in a new thread.

Hence my going to AsyncTask

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
kveola92
  • 69
  • 4

1 Answers1

0

You forgot to start the thread.

run() method doesn't create a new thread but runs the runnable instance in the main thread.start()creates a new thread instance. So Create a new background thread like this

Thread thread = new Thread() {
    @Override
    public void run() {
       //your code
    }
};

thread.start();
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20