2014-11-08 83 views
0

這是我的啓動畫面,如果我在按下主頁或多任務/應用程序切換按鈕時,意圖啓動應用程序崩潰,在logcat是致命的EXEPTION:螺紋1277。當玩家按主頁按鈕時,我可以殺死/刪除這個意圖嗎?意圖啓動畫面

public class SplashScreen extends Activity { 
private static int loadingTime = 1000; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

    setVolumeControlStream(AudioManager.STREAM_MUSIC); 

    setContentView(R.layout.loading_screen); 

    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      Intent i = new Intent(SplashScreen.this, MainActivity.class); 
      startActivity(i); 
      finish(); 
     } 
    }, loadingTime); 
} 
} 
+0

上帝的方式告訴你,splashscreens是邪惡的。 http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/ – Simon 2014-11-08 20:40:30

回答

0

以下代碼跟蹤SplashActivity是否至少部分顯示。如果是的話,它將繼續到MainActivity。如果沒有(按Back按鈕完成活動,則通過按Home按鈕停止活動),則不會發生任何事情。

該解決方案使用Fragment s,因此時間跨越例如,屏幕方向改變(無論您旋轉設備多少次,它都會持續指定的時間 - 定時器不會重置)。

public class SplashActivity extends Activity { 
    // tracks when the activity is at least partially visible (e.g. under a dialog) 
    private boolean mStarted = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // your current code 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    setVolumeControlStream(AudioManager.STREAM_MUSIC); 
    setContentView(R.layout.activity_startup); 

    if (savedInstanceState == null) { 
     // first time onCreate, create fragment which starts countdown 
     getFragmentManager() 
      .beginTransaction() 
      .add(SplashFinishFragment.newInstance(), SplashFinishFragment.TAG) 
      .commit(); 
    } else { 
     // fragment already set up from first onCreate after screen rotation 
    } 
    } 

    @Override 
    protected void onStart() { 
    // the activity becomes at least partially visible 
    mStarted = true; 

    super.onStart(); 
    } 

    @Override 
    protected void onStop() { 
    // the activity is no longer visible 
    mStarted = false; 

    super.onStop(); 
    } 

    public boolean isStarted2() { 
    // there is already hidden method isStarted() in the framework 
    // you can't use it and are not allowed to override it 
    return mStarted; 
    } 

    public static class SplashFinishFragment extends Fragment { 
    private static final String TAG = SplashFinishFragment.class.getSimpleName(); 

    private static final int DELAY = 1000; // one second delay 

    private static final Handler mHandler = new Handler(); // one main thread anyway 

    private final Runnable mRunnable = new Runnable() { 
     @Override 
     public void run() { 
     if (getActivity() == null) { 
      // this should never happen, there is no activity, so no fragment 
      Log.e(TAG, "No activity!"); 
      return; 
     } 

     SplashActivity a = (SplashActivity) getActivity(); 

     if (a.isStarted2() || a.isChangingConfigurations()) { 
      // if activity is even partially visible or is rotating screen right now, continue 
      Intent i = new Intent(a, SettingsActivity.class); 
      a.startActivity(i); 
     } 

     // in any case close splash 
     a.finish(); 
     } 
    }; 

    public static SplashFinishFragment newInstance() { 
     return new SplashFinishFragment(); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // the countdown will continue (not reset) across screen rotations 
     setRetainInstance(true); 

     // try running the main activity after specified time 
     mHandler.postDelayed(mRunnable, DELAY); 
    } 

    @Override 
    public void onDestroy() { 
     // if the fragment gets destroyed (e.g. activity closes) do not launch main activity 
     mHandler.removeCallbacks(mRunnable); 

     super.onDestroy(); 
    } 
    } 
} 

這是在虛擬Galaxy S2上測試的。當按下主頁或後退按鈕時,它將起作用。按下最近應用按鈕時它不起作用。我不知道你的使用案例,但我個人認爲,當我瀏覽最近的應用時,應用會繼續啓動。