17

我正在开发一个安卓应用程序。我想动态更改开始活动。我的意思是,当用户第一次启动应用程序时,启动活动会有所不同,而当第二次启动时,启动活动会发生变化。这将跳过前两个活动并移至第三个活动。我该如何实现这一点。

4

7 回答 7

37

您不能动态更改第一个活动,但您可以创建一个透明的活动,如下所示:

<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();
于 2013-03-29T06:26:00.613 回答
2

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 / SecondClass):

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
于 2017-05-26T09:07:47.380 回答
1

您可以根据您的要求使用SharedPreference 。

您可以从此链接存储和检索值

在 Activity 的每个Oncreate()方法中,您都可以检查 SharedPreference 值并在那里启动您的 Activity。

希望它会帮助你。

于 2013-03-29T05:24:25.057 回答
0

使用首选项存储您想要的值(条件)。然后根据那个改变startActivity。

于 2013-03-29T05:20:19.513 回答
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;
    }

在第一个活动中

SharedPreferences.Editor editor = mPreferences.edit();
                editor.putString("Name", edt.getText().toString()); 
editor.commit();
Intent i = new Intent(first.this, second.class);
                        startActivity(i);
于 2013-03-29T05:29:38.600 回答
0

当用户在登录屏幕上选择了记住我复选框时,我会这样做。

要将值保存到SharedPreference文件中:

你需要在第一次Activity和第二次中做一次这样的事情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();

要从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();
}

这些是必要的全局变量/实例用于Activity

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

如果您需要任何帮助,请告诉我。

于 2013-03-29T05:32:59.053 回答
-1

无论什么首先在主活动中打开您的应用程序。同时使用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");
    }


}
于 2013-03-29T05:57:55.443 回答