How to start a different activity with some delay after pressing a button in android?

后端 未结 7 720
感动是毒
感动是毒 2020-12-30 04:14

I want that a new activity should start with some delay on pressing a button. Is it possible to do that , and whats the procedure.

7条回答
  •  粉色の甜心
    2020-12-30 04:30

    Use a postDelayed() call with a runnable that launches your activity. An example code could be

        //will care for all posts
        Handler mHandler = new Handler();
    
        //the button's onclick method
        onClick(...)
        {
            mHandler.postDelayed(mLaunchTask,MYDELAYTIME);
        }
    
        //will launch the activity
        private Runnable mLaunchTask = new Runnable() {
            public void run() {
                Intent i = new Intent(getApplicationContext(),MYACTIVITY.CLASS);
                startActivity(i);
            }
         };
    

    Note that this lets the interface remain reactive. You should then care for removing the onclick listener from your button.

提交回复
热议问题