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

前端 未结 2 471
天命终不由人
天命终不由人 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:45

    You should remove this Thread.sleep(3000); which block the ui thread. You should never block the ui thred. You can use a Handler postDelayed with a delay and then startActivtiy.

    Handler handler = new Handler();
    handler.postDelayed(new Runnable(){
    @Override
      public void run(){
       // do something
     }
     }, 3000);
    

    To go back to previous Activity you can call finish().

    Also if you need to go back to the previous activity for 3 seconds why do you need

    Intent i = new Intent(this,myActivity.class);
    startActivity(i);
    

    Calling finish() will do the job

提交回复
热议问题