2010-07-08 67 views
1

我有一個問題,那就是我的SplashScreen。它是作爲一個介紹而建立的,3秒之後它顯示了程序的主菜單。 無論如何,如果在SplashScreen顯示的時間內按下Back或Home按鈕,它會關閉,但在SplashScreen之後,我選擇遵循的活動在三秒後仍會運行。如果主頁按鍵或按下後按下停止以下活動 - Android

我的代碼:*****更新的代碼*****

Handler ur = new Handler(); 
    myRun = new Runnable() { 
      public void run() {  
        mainIntent = new Intent(SplashScreen.this,MyApp.class); 
        SplashScreen.this.startActivity(mainIntent); 
        SplashScreen.this.finish(); 
        overridePendingTransition(R.anim.fadein, 
          R.anim.fadeout);  
      } 
    }; 
    ur.postDelayed(myRun, SPLASH_DISPLAY_TIME); 
} 

protected void onStop() { 
    super.onStop(); 
    ur.removeCallbacks(myRun); 
} 

即使我在這個閃屏的的onStop(),接下來的活動將仍然SPLASH_DISPLAY_TIME後運行。

因爲我改變了代碼後,我按了主頁按鈕後,我得到了強制關閉,並且SplashScreen消失了,我也無法啓動我的第二個活動。

回答

2

嘗試做這樣:

private static final int SPLASH_DISPLAY_TIME = 3000; 
Handler ur = new Handler(); 
Runnable yourRunnable = new Runnable() {  
     public void run() {  
       mainIntent = new Intent(SplashScreen.this,MyApp.class); 
       SplashScreen.this.startActivity(mainIntent); 
       SplashScreen.this.finish(); 
       overridePendingTransition(R.anim.fadein, 
         R.anim.fadeout);  
     } 
}; 
ur.postDelayed(yourRunnable, SPLASH_DISPLAY_TIME); 

然後,在某個地方或的onDestroy任何方法使用,以檢測當你的閃屏活動得到休息:

// somewhere 
ur.removeCallbacks(yourRunnable); 
+0

它的工作原理!公認! – Curtain 2010-07-28 16:30:34

相關問題