2014-02-07 46 views
1

我有一個android應用程序。在第一次運行時,第一個活動將打開並將在應用程序的存儲中保存一個值。當我重新打開應用程序時,我想跳過第一個活動。我怎樣才能做到這一點?Android-如何跳過第一個活動

當重新打開:

if(stogage_value == "enable"){ 
    skip first activity; 
}else{ 
first activity; 
} 
+0

http://stackoverflow.com/q/16419627/1765530 – appukrb

回答

1

節省首家推出布爾標誌sharedpreference

現在閱讀活動啓動前該值查布爾值

所以你的代碼會是這樣的

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    .... 
Boolean flag; 
loadSavedPreferences(); 
Intent myIntent; 
if(flag){ 
myIntent = new Intent(CurrentActivity.this, FirstActivity.class); 
savePreferences() 
}else{ 
myIntent = new Intent(CurrentActivity.this, NextActivity.class); 

private void loadSavedPreferences() { 
    SharedPreferences sharedPreferences = PreferenceManager 
      .getDefaultSharedPreferences(this); 
    flag = sharedPreferences.getBoolean("FirstLaunch", true); 

} 

private void savePreferences() { 

    SharedPreferences sharedPreferences = PreferenceManager 
      .getDefaultSharedPreferences(this); 
    Editor editor = sharedPreferences.edit(); 
    editor.putBoolean("FirstLaunch", false); 
    editor.commit(); 

} 
    } 

這裏是slidenerd

1

後共享preference.And該值每一次檢查第一次運行存儲標誌值,如果不是第一次運行時調用第二個活動。

1
Intent myIntent; 
if(stogage_value == "enable"){ 
    myIntent = new Intent(CurrentActivity.this, NextActivity.class); 
}else{ 
    myIntent = new Intent(CurrentActivity.this, FirstActivity.class); 
} 
CurrentActivity.this.startActivity(myIntent); 
1

您需要使用共享偏好來維護計數器或標誌。只需在第一次調用活動時將標誌設置爲true,然後在標誌爲true時調用另一個活動或執行其他操作。請參閱提供的答案here

1

在OnCreate中,你可以在此實現way.You必須拯救「stogage_value」優先

boolean firstTime = ""; //get this value from preference 
    if(!firstTime){ 
     Intent myIntent = new Intent(First.this, NextActivity.class); 

     firstTime = false; 
     // here save this in preference 
    }else{ 
     setContentView(R.layout.main); 
    } 
1

共享偏好視頻教程,我只需運行新的活動,停止第一項活動

if(stogage_value == "enable"){ 
    Intent i = new Intent(context, SecondActivity.class); 
    startActivity(i); //start second activity 
    finish(); //finish first activity 
}else{ 
    //do nothing 
} 
相關問題