2013-03-29 64 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); 
}