2013-03-29 117 views
13

我正在開發一個android應用程序。我想動態更改開始活動。我的意思是,當用戶第一次啓動應用程序,然後啓動活動將不同,當第二次啓動活動change.This將跳過前兩個活動,並轉移到第三個活動。我可以實現這一點。如何動態更改開始活動?

回答

0

使用首選項來存儲您想要的值(條件)。然後根據那個改變startActivity。

1

您可以根據您的要求使用SharedPreference

可以存儲和檢索該Link

值內,您的每個活動Oncreate()方法,你可以檢查SharedPreference值並且啓動活動。

希望它能幫助你。

0

使用sharedpreference到他們第一次登錄或沒有

if (!checkNameInfo()) { 
//first time 
        FirstActivity(); 
       } else { 
//second time 
        Intent i = new Intent(first.this, second.class); 
        startActivity(i); 
        finish(); 
       } 

檢查值

private final boolean checkNameInfo() { 
     boolean role = mPreferences.contains("Name"); 
     if (role) { 
      return true; 
     } 
     return false; 
    } 

IN firstActivity

SharedPreferences.Editor editor = mPreferences.edit(); 
       editor.putString("Name", edt.getText().toString()); 
editor.commit(); 
Intent i = new Intent(first.this, second.class); 
         startActivity(i); 
0

這是當用戶選擇了我做什麼在登錄屏幕上記住我複選框。

要保存價值的SharedPreference文件:

您需要在第二Activity

sharedPrefs = getApplicationContext().getSharedPreferences(PRIVATE_PREF, Context.MODE_PRIVATE); 

// EDITOR INSTANCE TO SAVE THE NAG SETTING 
editor = sharedPrefs.edit(); 

// GET THE NAG SETTING CHECKBOX 
if (chkbxNagSetting.isChecked()) { 

    editor.putBoolean(NAG_SETTING, true); 
} else { 
    editor.putBoolean(NAG_SETTING, false); 
} 

editor.commit(); 

做這樣的事情曾經在第一Activity和一次是從SharedPreference文件檢索值:

boolean blNagSetting = sharedPrefs.getBoolean(NAG_SETTING, false); 

if (blNagSetting == true) { 
    Intent startMainPage = new Intent(SignIn.this, SplashScreen.class); 
    startMainPage.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(startMainPage); 
    finish(); 
} 

而這些是在中使用的必要的全局變量/實例:

SharedPreferences sharedPrefs; 
Editor editor; 
private static final String PRIVATE_PREF = "CHANGE_TO_SOME_FILE_NAME"; 
private static final String NAG_SETTING = "nag_setting"; 

你需要稍微修改代碼以佔跳過2 Activities

讓我知道你是否需要任何幫助。

0

無論首先在主要活動中打開您的應用程序。同時使用SharedPreference來保存您已加載應用程序的次數。

你要去必須在下面做一些事情你

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    String dataAvailable; 
    SharedPreferences prefs = getSharedPreferences("countPref", Context.MODE_PRIVATE); 
    dataAvailable = prefs.getString("dataAvailable", null); 

    //checking whether launching for the first time. 
    if(dataAvailable!=null){ 
     int appLoadedCount = prefs.getInt("appLoadedCount", -1); 
     appLoadedCount++; 
     prefs.edit().putInt("appLoadedCount", appLoadedCount).commit(); 

     // Check how many times loaded 
     if(appLoadedCount==0){ 
      Intent firstAct = new Intent(MainActivity.this, FirstActivity.class); 
      startActivity(firstAct); 
     } 
     else if(appLoadedCount==1){ 
      Intent scndAct = new Intent(MainActivity.this, ScndActivity.class); 
      startActivity(scndAct); 
     } 
     else if(appLoadedCount==2){ 
      Intent thirAct = new Intent(MainActivity.this, ThirdActivity.class); 
      startActivity(thirAct); 
     } 
     else{ 
      Intent thirAct = new Intent(MainActivity.this, ThirdActivity.class); 
      startActivity(thirAct); 
     } 

     Log.v("avilable", dataAvailable); 
     Log.v("avilable", String.valueOf(appLoadedCount)); 
    } 
    else{ 
     //loading first time 
     prefs.edit().putString("dataAvailable", "yeap").commit(); 
     //setting the count to 1 as loaded for the firs time 
     prefs.edit().putInt("appLoadedCount", 0).commit(); 
     Log.v("Not avilable", "Loaded first time"); 
    } 


} 
28

不能動態改變的第一個活動,但您可以創建這樣一個透明的活動:

<activity 
    android:name=".ActivityLauncher" 
    android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" > 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

,並選擇下一個活動在onCreate方法中:

if (logged()) { 
    intent = new Intent(this,MainActivity.class); 
} else { 
    intent = new Intent(this,SignInActivity.class); 
} 
startActivity(intent); 
finish(); 
+0

它運作良好。 –

+0

它需要佈局嗎? –

0

它不是必需的ary一個Activity必須有一個佈局文件。您可以在啓動器活動中進行條件檢查,並根據條件重定向到其他活動。 (雖然從發射器活動到條件活動的轉換將不可見)。

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
Intent intent; 
if (condition) { 
    intent = new Intent(this, FirstClass.class); 
} else { 
    intent = new Intent(this, SecondClass.class); 
} 
startActivity(intent); 
finish(); 
// note we never called setContentView() 
} 

其他活動(的Firstclass /二等):

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
}