如何避免启动前出现黑屏?

发布于 2024-11-25 12:53:21 字数 827 浏览 1 评论 0原文

我创建了一个带有背景和进度条的加载页面,当它完成加载时,它启动主类,但加载屏幕没有显示。加载时只是黑屏,然后是主屏幕。我把所有的工作都放在 onResume 中,我也尝试过 onStart 但没有运气,

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loadpage);
    //get rid of title bar
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(1);

}

@Override
public void onResume() {
    super.onResume();

    words = WordList.sharedWordList(this);

    if(generatedLevels==null)
    {
        generatedLevels  = new ArrayList<PuzzleMZLen>();
    }
    if(!p.isAlive())
    {
        p.start();          
    }


    Intent i = new Intent(getApplicationContext(), Main.class);
    startActivity(i);

}

提前谢谢

I created a loadpage with a background and a progress bar and when it finishes loading, it starts the main class but the loading screen is not showing up. It is just a black screen while it loads and then the main screen. I put all the work in the onResume and I also tried onStart with no luck

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loadpage);
    //get rid of title bar
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(1);

}

@Override
public void onResume() {
    super.onResume();

    words = WordList.sharedWordList(this);

    if(generatedLevels==null)
    {
        generatedLevels  = new ArrayList<PuzzleMZLen>();
    }
    if(!p.isAlive())
    {
        p.start();          
    }


    Intent i = new Intent(getApplicationContext(), Main.class);
    startActivity(i);

}

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满身野味 2024-12-02 12:53:22

您必须使用 AsyncTask 来完成此类工作。您的布局已填充加载完成后。所以你看到的是黑屏。

使用AsyncTask类的onPreExecute来显示进度条。并在doInBackground方法中编写加载代码。

You have to use AsyncTask for this kind of work.Your layout is populated after the completion of the loading.So you are watching black screen.

Use onPreExecute of AsyncTask class to show the progress bar. And write loading code in the doInBackground method.

何必那么矫情 2024-12-02 12:53:22

一定要使用 AsyncTask 来实现这一点。我的应用程序也有同样的情况,这是我的代码:

private class getAllData extends AsyncTask<Context, Void, Cursor> {
    protected void onPreExecute () {
        dialog = ProgressDialog.show(Directory.this, "", 
                "Loading. Please wait...", true);
    }

    @Override
    protected Cursor doInBackground(Context... params) {
        DirectoryTransaction.doDepts();

        return null;
    }

    protected void onPostExecute(Cursor c) {

        for(int i = 0 ; i < DeptResults.length ; i++){
            deptsAdapter.add(DeptResults[i][0]);            
        }
        dialog.dismiss();
    }
}

onPreExecute 方法加载一个仅显示“正在加载。请稍候...”的 ProgressDialog。 doInBackground 执行使我的应用程序加载的操作(在我的例子中,从服务器抓取和解析文本),然后 onPostExecute 填充旋转器,然后关闭 ProgressDialog。您可能希望进度条有一些不同的东西,但 AsyncTask 将非常相似。在 onCreate 中使用 new getAllData.execute(this); 调用它

Definitely use AsyncTask for this. I had the same thing going for my app, and here's my code:

private class getAllData extends AsyncTask<Context, Void, Cursor> {
    protected void onPreExecute () {
        dialog = ProgressDialog.show(Directory.this, "", 
                "Loading. Please wait...", true);
    }

    @Override
    protected Cursor doInBackground(Context... params) {
        DirectoryTransaction.doDepts();

        return null;
    }

    protected void onPostExecute(Cursor c) {

        for(int i = 0 ; i < DeptResults.length ; i++){
            deptsAdapter.add(DeptResults[i][0]);            
        }
        dialog.dismiss();
    }
}

onPreExecute method loads a ProgressDialog that just shows "Loading. Please wait...". doInBackground does what was making my app load (in my case grabbing and parsing text from a server) and then onPostExecute is filling a spinner then dismissing the ProgressDialog. You'll want some thing different for a progress BAR, but the AsyncTask will be very similar. Call it in onCreate with new getAllData.execute(this);

自由范儿 2024-12-02 12:53:22

有一个很好的方法来创建良好的飞溅活动。

为了防止 ANR,您应该将所有数据加载移到 UI 线程之外,正如其他答案中提到的那样。请使用 AsyncTask 或任何其他类型的多线程。

然而,要消除在某些慢速设备上显示片刻的烦人黑屏,您需要执行后续步骤:

  1. 创建 bg_splash 可绘制对象。它将及时显示给用户,而不是黑色背景。例如,它可以是品牌颜色背景上的品牌标志。
  2. 创建闪屏主题并将其放入/res/values/themes.xml

    
    <资源>   
        <样式名称=“MyApp.Splash”父=“@style/Theme.Sherlock.Light.NoActionBar”>
            <项目名称=“android:windowBackground”>@drawable/bg_splash
        
    
    
  3. 不要忘记通过更新 将创建的主题分配给闪屏活动>AndroidManifest.xml

    
    
    
        ...
    
        <应用>
            <活动
                android:name=".SplashActivity"
                android:theme="@style/MyApp.Splash" >
                <意图过滤器>
                    <动作 android:name="android.intent.action.MAIN" />
                    <类别 android:name="android.intent.category.LAUNCHER" />
                
            <活动>
            ...
        
    
    

There is a good way how to create a good splash activity.

To prevent ANR you should move all data loading outside of UI thread as it was mentioned at other answers. Please use AsyncTask or any other kind of multi-threading for that.

However to remove annoying black screen which shows for few moments on some slow devices you need to do next steps:

  1. Create bg_splash drawable. It will be shown instead of black background just in time activity shows to user. For example it can be brand logo on brand color background.
  2. Create a splash screen theme and put it into /res/values/themes.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>   
        <style name="MyApp.Splash" parent="@style/Theme.Sherlock.Light.NoActionBar">
            <item name="android:windowBackground">@drawable/bg_splash</item>
        </style>
    </resources>
    
  3. Don't forget assign created theme to splash activity by updating AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.myapp"
        android:versionCode="1"
        android:versionName="1" >
    
        ...
    
        <application>
            <activity
                android:name=".SplashActivity"
                android:theme="@style/MyApp.Splash" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            <activity>
            ...
        </application>
    </manifest>
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文