问题描述
我希望我的活动显示3秒的屏幕,然后返回上一屏幕。但是当我使用
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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_layout);
TextView tvResult = (TextView)findViewById(R.id.textView1)
Thread.sleep(3000);
Intent i = new Intent(this,myActivity.class);
startActivity(i);
但不幸的是,这不起作用。此事件显示活动等待3秒后返回。但是,我希望它在返回之前显示其内容。我该怎么办?
But unfortunately, this does not work. This doesent show the activity waits 3 seconds and goes back. However, i want it to show its contents before going back. How can i do it ?
推荐答案
你应该删除这个 Thread.sleep(3000);
阻止ui线程。你永远不应该阻止ui thred。您可以使用Handler post延迟延迟,然后startActivtiy。
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);
要返回上一个活动,您可以致电 finish()
。
To go back to previous Activity you can call finish()
.
此外,如果您需要返回上一个活动3秒,为什么还需要
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);
调用完成()
将完成工作
这篇关于如何让应用等待,然后开始活动或回去?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!