开发者

How do I avoid a black screen before startup?

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.isA开发者_StackOverflowlive())
    {
        p.start();          
    }


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

}

thanks in advance


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.


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);


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>
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜