Reputation: 623
I have built a SplashActivity
to be displayed for 5500
ms before the MainActivity
. But when the app runs, it just shows a white screen for 2s, then jumps to the MainActivity
. No error is being displayed, but there is no splash screen design or animation or anything else.
Here is the activity in AndroidManifest.xml
:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".SplashActivity"></activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
This is my SplashActivity
:
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
public class SplashActivity extends AppCompatActivity {
TextView appName;
Animation frombottom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, 5500);
appName = (TextView) findViewById(R.id.appName);
frombottom = AnimationUtils.loadAnimation(this, R.anim.frombottom);
appName.setAnimation(frombottom);
}
}
What could be the possible problem and the solution to that? Thanks in advance for your suggestion.
Upvotes: 0
Views: 209
Reputation: 297
In Launcher you have called MainActivity thats the reason your splash screen open
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:launchMode="singleTask"></activity>
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Upvotes: 2
Reputation: 4737
Making an Activity
to do a "fixed" splash screen is not the right way to go.
Use an drawable or make one from xml, for example (bg_launch.xml):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="@color/primary"/>
</item>
<item
android:drawable="@mipmap/ic_launcher"
android:gravity="center"/>
</layer-list>
From you main theme create a launch theme
<style name="AppTheme.Launcher">
<item name="android:windowBackground">@drawable/bg_launch</item>
<item name="android:windowFullscreen">true</item>
</style>
then from you manifest apply this theme to your main Activity
<activity
android:name=".MainActivity"
...
android:theme="@style/AppTheme.Launcher">
...
Finally revert you your default theme when app start from your main Activity
@Override
void onCreate(savedInstanceState: Bundle){
setTheme(R.style.AppTheme)
super.onCreate(savedInstanceState)
}
Doing this way you'll have no white screen delay, you can still go for an Activity
splash screen if you need animation or translated text but you'll get that drawback to have a little delay before it's actually present to user.
Upvotes: -1
Reputation: 177
Manifest file needs to be changed
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask">
</activity>
</application>
Upvotes: 0