当我运行应用程序时,SplashScreen在屏幕上闪烁一小秒,然后完全消失3秒钟。完成3秒后,MainActivity启动。问题是如何在没有消失的情况下显示我的启动画面?
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();
}}
清单文件
<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>
答案 0 :(得分:3)
更改此
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
}
}, SPLASH_TIME_OUT);
SplashScreen.this.finish();
到
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
SplashScreen.this.finish();
}
}, SPLASH_TIME_OUT);
也无需在onBackPressed
@Override
public void onBackPressed() {
SplashScreen.this.finish(); // Remove this
super.onBackPressed();
}
答案 1 :(得分:1)
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();代码行完成你的SplashActivity,你需要在3秒后执行此操作,这就是为什么它在处理程序线程中并在3秒后执行。
答案 2 :(得分:0)
在开始SplashScreen
之前,只是不要完成MainActivity
。
只需从SplashScreen.this.finish();
课程中删除SplashScreen
行即可。
答案 3 :(得分:0)
public void run() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
finish()
}
}, SPLASH_TIME_OUT);
而不是
public void run() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
}
}, SPLASH_TIME_OUT);
SplashScreen.this.finish();
答案 4 :(得分:0)
快速修复:
删除
SplashScreen.this.finish();
改变之类,
public void run() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
finish();
}