Reputation: 53
Part of my project I want use a splash screen. Everything is fine with the code, but unfortunately the splash screen doesn't load at the starting. My manifest file is
<activity
android:label="@string/app_name"
android:name="com.example.GamePlay.SplashScreen"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"
android:screenOrientation="landscape"> </activity>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity
android:name="com.example.GamePlay.Game"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"
android:screenOrientation="landscape"
android:label="@string/app_name"></activity>
SplashScreen.java
public class SplashScreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Thread welcomeThread = new Thread() {
int wait = 0;
@Override
public void run() {
try {
super.run();
while (wait < 2000) {
sleep(100);
wait += 100;}
} catch (Exception e) {
System.out.println("SplashScreen()"+e.toString());
} finally {
System.out.println("Final statment for run()");
} } };
welcomeThread.start();
new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params) {
LayoutInflater inflater = (LayoutInflater) SplashScreen.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Game.cachedView = inflater.inflate(R.layout.activity_game, null, false);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
finish();
Intent intent = new Intent(getBaseContext(), Game.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
}.execute();
} }
Please help me to fix this problem.
Upvotes: 0
Views: 1280
Reputation: 57
Sometimes you can have a great code and the problem may be in the IDE you're using. If on IDE's such as IntelliJ IDEA, just load the app onto the AVD(hopefully it successfully runs-but bypasses the splashscreen), then reload the AVD (or close it and start it up again).Launch the app from the AVD-do not build it again-from the IDE. You'll notice the spalshscreen will be displayed with the delay time that you set. Hope this helps you and happy Android Coding. ☺.
Upvotes: 0
Reputation: 96
Basic steps to display splash:-
1.Desgin Your Splash XML.
2.Add Your splash.java File.
3.In last call It on AndroidMainfest File.
In Your Java File Include Intent Like this:-
public class SplashScreen extends Activity {
private long splashDelay = 5000; //5 seconds
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
TimerTask task = new TimerTask()
{
@Override
public void run() {
finish();
Intent mainIntent = new Intent().setClass(SplashScreen.this, SplashActivity.class);
startActivity(mainIntent);
}
};
Timer timer = new Timer();
timer.schedule(task, splashDelay);
}
}
Upvotes: 0
Reputation: 16181
I believe you need to put that intent filter inside your activity tag.
At the moment its not tied to an activity and therefore doesn't load at launch.
<activity
android:label="@string/app_name"
android:name="com.example.GamePlay.SplashScreen"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Upvotes: 2
Reputation: 4651
you should close the activity after the intent-filter and second activity had no intent-filter ..
<activity
android:label="@string/app_name"
android:name="com.example.GamePlay.SplashScreen"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.GamePlay.Game"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"
android:screenOrientation="landscape"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.GAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Upvotes: 1