I have a splash screen which is being showed only on fresh start of app. If user hit back button and start app again the splash doesn't show. Everything is fine until here, if splash doesn't show, there is a 1-2 second black screen when opening app. Here is my splashactivity java file;
public class SplashScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false)) // if first time, show splash
{
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
setContentView(R.layout.activity_splash);
Thread t = new Thread() {
public void run() {
try {
int time = 0;
while (time < 4000) {
sleep(100);
time += 100;
}
}
catch (InterruptedException e) {
// do nothing
}
finally {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}
};
t.start();
}
else // if not first time, dont show splash
{
setContentView(R.layout.activity_splash);
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
How can I fix this issue ?
Because you are checking if it is the "first time" opening the app before showing the splash screen:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false)) {
// Show splash
} else {
// Don't show splash
}
This is not the right way to create a splash screen in the first place. You shouldn't make your users wait for the splash screen by creating a timer. Instead, you should only show the splash screen for as long as it takes your app to load.
To do this you should create a simple layer-list drawable to use as the background of your splash activity:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- A solid background fill -->
<item
android:drawable="@color/gray"/>
<item>
<!-- A centered logo -->
<bitmap
android:gravity="center"
android:src="@mipmap/ic_launcher"/>
</item>
</layer-list>
Then use this layer-list as the background
for the theme you will use in your splash activity:
<resources>
<!-- Base application theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<!-- Splash Screen theme -->
<style name="SplashTheme">
<item name="android:background">@drawable/background_splash</item>
<item name="android:windowAnimationStyle">@null</item>
</style>
</resources>
Apply that theme to your splash activity in the manifest:
<activity android:name=".SplashScreen"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Then, finally, all you have to do in your splash activity is start your MainActivity
:
public class SplashScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
Make sure you don't call setContentView()
in your splash activity, as this will add unnecessary loading time to your splash screen.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments