问题描述
我的应用程序MainActivity
和BannerActivity
中有两个活动。从主要活动中,我在BannerActivity
方法中启动onCreate
。但是我首先看到MainActivity
屏幕,然后再看到BannerActivity
屏幕。
题
我该怎么做才能使BannerActivity
首先显示,并且倒数计时器将停止,并且BannerActivity
将在MainActivity
出现在屏幕后关闭。
主要活动
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Show banner activity for several seconds then close it. */
Intent bannerIntent = new Intent(MainActivity.this, BannerActivity.class);
this.startActivity(bannerIntent);
}
};
BannerActivity
public class BannerActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
/* Make banner fullscreen. */
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_banner);
/* Launch count down timer for several seconds. */
CountDownTimer countDownTimer = new CountDownTimer(3000, 1000) {
@Override
public void onTick(long millisUntilFinished) { /* Not used. */ }
@Override
public void onFinish() {
BannerActivity.this.finish();
}
}.start();
}
@Override
public void onBackPressed() {
/* Lock back button presses. */
super.onBackPressed();
}
};
最佳答案
您需要一个SplashScreen:
http://www.thiagorosa.com.br/en/tutorial/part01-splash-screen
public class GameSplash extends Activity {
// time to wait on the splash screen
private static final int SPLASH_SCREEN_DELAY = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Thread() {
@Override
public void run() {
try {
// do any heavy initialization here
// wait a few seconds before going to the next screen
sleep(SPLASH_SCREEN_DELAY);
}
catch (InterruptedException e) {
}
catch (Exception e) {
}
finally {
// start the level selection screen
Intent intentSelect = new Intent(GameSplash.this, GameSelect.class);
startActivity(intentSelect);
}
}
}.start();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// ignore any key press on the splash screen
return true;
}
}