简体   繁体   English

如何在onCreate完成之前显示内容 - Android

[英]How to display stuff before onCreate is finished - Android

I would like to create a splash screen for an app I hope to make. 我想为我希望制作的应用程序创建一个启动画面。 My first solution was to display a new layout for a few seconds and then display a new layout like so: 我的第一个解决方案是显示一个新的布局几秒钟,然后显示一个新的布局,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.logo)
    android.os.SystemClock.sleep(5000);
    setContentView(R.layout.activity_main);
}

But the problem is that it displays a white screen for 5 seconds then goes to my main screen. 但问题是它显示白色屏幕5秒然后进入我的主屏幕。 I Thought I could tackle this problem by creating a new method or class and change the layout from there but using a method did nothing and creating a class only proved to be more difficult because I'm pretty sure I'd have to use AsyncTask. 我认为我可以通过创建一个新的方法或类来解决这个问题,并从那里改变布局,但是使用一个方法什么都没做,只是创建一个类被证明更难,因为我很确定我必须使用AsyncTask。 I feel like there is a very easy solution I don't know about. 我觉得有一个我不了解的非常简单的解决方案。 Thanks in advance, Mr. Schmuck 先生,谢谢先生

you need to create another activity for splash screen 你需要为启动画面创建另一个活动

Example: 例:

public class SplashScreen extends Activity {

    // Splash screen timer
    private static int SPLASH_TIME_OUT = 3000;

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

        new Handler().postDelayed(new Runnable() {  
            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);
    }

}

Here is the full example 是完整的例子

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM