Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

android - blank screen comes before splash

The main problem is the splash screen appears after 2-3 seconds. Before splash screen a blank layout appears which I don't want. Otherwise it runs fine. Just want to remove the blank layout which appears before the splash page.

MainActivity:

public class MainActivity extends Activity {

    private static String TAG = MainActivity.class.getName();
    private static long SLEEP_TIME = 5; // Sleep for some time

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  // Removes notification bar

        setContentView(R.layout.activity_main);

        // Start timer and launch main activity
        IntentLauncher launcher = new IntentLauncher();
        launcher.start();
    }

    private class IntentLauncher extends Thread {

        @Override
        /**
         * Sleep for some time and than start new activity.
         */
        public void run() {
            try {
                // Sleeping
                Thread.sleep(SLEEP_TIME*1000);
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }

            // Start main activity
            Intent intent = new Intent(MainActivity.this, Login.class);
            MainActivity.this.startActivity(intent);
            MainActivity.this.finish();
        }
    }

}

main layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/splash"
    tools:context=".MainActivity" >

</RelativeLayout>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Generally speaking, splash screens are not recommended for an app but if you really must.

Android will load a blank layout before it loads an activity layout based on the theme you have set for it. The solution is to set the theme of the splash activity to a transparent one.

Create a transparent theme in res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="Theme.Transparent" parent="android:Theme">
      <item name="android:windowIsTranslucent">true</item>
      <item name="android:windowBackground">@android:color/transparent</item>
      <item name="android:windowContentOverlay">@null</item>
      <item name="android:windowNoTitle">true</item>
      <item name="android:windowIsFloating">true</item>
     <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>

Then set the theme in your manifest

<activity android:name=".SplashActivity" android:theme="@style/Theme.Transparent">
...
</activity>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...