Search code examples
androidthread-sleep

android - Thread.sleep causes lags in app


I want to do app to show 3 elements (each 2 seconds) and each start showing after the before element. Here is code:

public class Remember extends Activity
{
    TextView text;
    ImageView element1, element2, element3, element4 ,element5, element6;
    Button button1, button2, button3, button4, button5, button6;
    int data, id;
Random random;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_remember);

    random = new Random();

    id=1;

    text = (TextView)findViewById(R.id.text);

    element1 = (ImageView)findViewById(R.id.element1);
    element2 = (ImageView)findViewById(R.id.element2);
    element3 = (ImageView)findViewById(R.id.element3);
    element4 = (ImageView)findViewById(R.id.element4);
    element5 = (ImageView)findViewById(R.id.element5);
    element6 = (ImageView)findViewById(R.id.element6);

    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);
    button5 = (Button) findViewById(R.id.button5);
    button6 = (Button) findViewById(R.id.button6);

    element1.setVisibility(View.GONE);
    element2.setVisibility(View.GONE);
    element3.setVisibility(View.GONE);
    element4.setVisibility(View.GONE);
    element5.setVisibility(View.GONE);
    element6.setVisibility(View.GONE);

    data = random.nextInt(6)+1;




}
public void button1(View v)
{
    gameStart();
}

public void gameStart()
{
    do{ 
    if (data==1)
    {
        CountDownTimer cdt = new CountDownTimer(2000, 1000)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                element1.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish()
            {
                element1.setVisibility(View.GONE);

            }
        }.start();

    }
    else if (data==2)
    {
        CountDownTimer cdt = new CountDownTimer(2000, 1000)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                element2.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish()
            {
                element2.setVisibility(View.GONE);

            }
        }.start();

    }
    else  if (data==3)
    {
        CountDownTimer cdt = new CountDownTimer(2000, 1000)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                element3.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish()
            {
                element3.setVisibility(View.GONE);

            }
        }.start();

    }
    else if (data==4)
    {
        CountDownTimer cdt = new CountDownTimer(2000, 1000)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                element4.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish()
            {
                element4.setVisibility(View.GONE);

            }
        }.start();

    }
    else if (data==5)
    {
        CountDownTimer cdt = new CountDownTimer(2000, 1000)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                element5.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish()
            {
                element5.setVisibility(View.GONE);

            }
        }.start();

    }
    else if (data==6)
    {
        CountDownTimer cdt = new CountDownTimer(2000, 1000)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                element6.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish()
            {
                element6.setVisibility(View.GONE);

            }
        }.start();

    }
    id=id+1;
    text.setText("cos " + id);
    data = random.nextInt(6)+1;
     try
    {
        Thread.sleep(2000);
    } catch (InterruptedException e)
    {
        e.printStackTrace();
    }
    }while(id<=3);
}

}

But if I clicked start application start lagging and everything go to norm after time which all elements should be hide. If I delete Thread.sleep all elements show in the same time. What should I do?


Solution

  • Thread.sleep(2000); on the ui thread blocks the ui thread. You should never block the ui thread. Remove the sleep.

    To create a delay use a Handler.

    Read

    http://developer.android.com/guide/components/processes-and-threads.html