问题
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>
回答1:
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>
回答2:
It's better to use a Themed background for your starting activity but if you don't want the blank screen appears before launching main activity you can define your activity like this:
Add android:windowDisablePreview
to your AppTheme in res/values/styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="android:Theme">
<item name="android:windowDisablePreview">true</item>
</style>
</resources>
Then set your activity theme in your manifest:
<activity android:name=".MainActivity" android:theme="@style/AppTheme">
...
</activity>
P.S: Setting android:windowDisablePreview
has no effect on your activity background, so you have nothing to worry.
回答3:
The problem you are facing here is called 'cold start' and it's the time mostly spent in your Application.onCreate
method, which usually does some initialisations and it might take longer than you wish. You can read here the official documentation: https://developer.android.com/topic/performance/launch-time.html
If this is the case than setting the theme to translucent or disabling preview as others suggested will only apparently solve the issue, because in reality you will try to launch the app and you won't receive any feedback about the fact the you tapped the app icon. You will see your app starting with a delay, which is not a desired UX.
Blank, black or white screen, it really depends on the android:windowBackground
attribute specified in your main activity theme.
The best you can do, apart from moving some of the initialisations that you probably are doing in your Application.onCreate
method, is to brand your preview window with a logo as suggested in this post:
https://plus.google.com/+AndroidDevelopers/posts/Z1Wwainpjhd
You can go further and improve the user experience by animating your logo image in a splash screen, if it's the case, or by designing the preview window in a way that can be smooth transitioned to your main activity content, like described here:
http://saulmm.github.io/avoding-android-cold-starts
Same question is answered correctly here: https://stackoverflow.com/a/40482549/7094200 and described in this blog post: https://www.bignerdranch.com/blog/splash-screens-the-right-way/
回答4:
Just mention the transparent theme to the starting activity in the AndroidManifest.xml file.
<activity
android:name="first Activity Name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
回答5:
Really! the below link work for me!!!!
https://cyrilmottier.com/2013/01/23/android-app-launching-made-gorgeous/
write the separate style in style.xml for your splash screen so that you can co-relate both the screen.
Herein the style:
<style name="AppTheme.Splash" parent="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/splash_final</item>
</style>
回答6:
It' s android features. You can change background color of your blankscreen. Use style:
<resources>
<style name="Theme" parent="android:style/Theme" />
<style name="Theme.MainTheme" parent="Theme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowBackground">@drawable/background_drawable</item>
</style>
</resources>
then use it in manifest:
<application
android:name="@string/app_name"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.MainTheme"
>
回答7:
With support for AppCompatActivity
res/values/styles.xml
<style name="SplashTheme" parent="AppTheme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowDisablePreview">true</item>
</style>
GL
Source
回答8:
I fixed mine by just updating the build tools in Android Studio.
回答9:
I think this is similar to some answers that have been posted above. I still would like to recommend the following Big Nerd Ranch guide on how to create a splash screen the right way because its well illustrated and easy to follow. You should really go there and read it instead of continuing below :). https://www.bignerdranch.com/blog/splash-screens-the-right-way/
But in short what it suggests is, at app start you launch an initial splash activity. For this activity you create an XML drawable (@drawable/background_splash
).
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/gray"/>
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/ic_launcher"/>
</item>
</layer-list>
Then you create a theme for splash activity’s and set the drawable you created above as its window background.
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
And then in the manifest you tell the splash activity to use the above theme.
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Finally in your splash activity implement theonCreate
method like below (thats all the code you need in the activity). This will launch your main activity and finish the splash activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
回答10:
Add this in your styles file
<item name="android:windowDisablePreview">true</item>
回答11:
Add this style
<style name="Theme.Transparent" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDisablePreview">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
If you load image in your splash screen than load Image from your layout, like below code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:background="@color/color_white">
<ImageView
android:id="@+id/ivSplash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/SPLASH_IMAGE"
/>
</LinearLayout>
来源:https://stackoverflow.com/questions/30342933/blank-screen-comes-before-splash