Reputation: 505
When i run the app, SplashScreen blinks for a fraction of second on the screen and disappears completely for 3 seconds. After completing 3 sec MainActivity launches. Question is how to display my splash screen with out disapearing?
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 3000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
}
}, SPLASH_TIME_OUT);
SplashScreen.this.finish();
}
@Override
public void onBackPressed() {
SplashScreen.this.finish();
super.onBackPressed();
}}
MANIFEST FILE
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="com.simbotix.guardianonthego.SplashScreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.simbotix.guardianonthego.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Upvotes: 5
Views: 8839
Reputation: 651
Quick Fix :
remove
SplashScreen.this.finish();
Change like,
public void run() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
finish();
}
Upvotes: 0
Reputation: 3110
public void run() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
finish()
}
}, SPLASH_TIME_OUT);
Instead of
public void run() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
}
}, SPLASH_TIME_OUT);
SplashScreen.this.finish();
Upvotes: 0
Reputation: 23638
Simply just do not finish your SplashScreen
before starting MainActivity
.
Just remove the SplashScreen.this.finish();
line from your SplashScreen
class.
Upvotes: 0
Reputation: 1305
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
SplashScreen.this.finish();
}
}, SPLASH_TIME_OUT);
SplashScreen.this.finish(); line of code finish your SplashActivity and you need to execute this after 3 seconds that's why it is in handler thread and executed after 3 seconds.
Upvotes: 2
Reputation: 14808
Change this
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
}
}, SPLASH_TIME_OUT);
SplashScreen.this.finish();
to
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
SplashScreen.this.finish();
}
}, SPLASH_TIME_OUT);
Also no need to finish Activity in onBackPressed
@Override
public void onBackPressed() {
SplashScreen.this.finish(); // Remove this
super.onBackPressed();
}
Upvotes: 4