How to make app wait and then start activity or go back?

前端 未结 2 470
天命终不由人
天命终不由人 2021-01-28 09:30

I want my activity to show a screen for 3 seconds, then go back to previous screen. But when i use

protected void onCreate(Bundle savedInstanceState) {
                 


        
2条回答
  •  执念已碎
    2021-01-28 09:43

    This is not the recommended way to do this. Using Thread.sleep you're blocking the main UI thread for 3000 milliseconds. This means that nothing in the activity will work until 3 seconds are passed.

    Instead, you could do this: edited: now it works well.

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_layout);
        TextView tvResult = (TextView)findViewById(R.id.textView1)
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    Thread.sleep(3000);
                    Intent i = new Intent(getApplicationContext(), myActivity.class);
                    startActivity(i);
                }
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
    }
    

提交回复
热议问题