更改 AndroidManifest.xml 以根据共享偏好设置 LAUNCHER activity

Change AndroidManifest.xml to set LAUNCHER activity base on shared preference

我有一个注册 activity 还有一个主 activity。

我想达到的效果:

我想:

1) 如果用户尚未注册

,则显示注册activity

2) 如果用户已经注册

,则显示主activity

我做了什么:

1) 我已经将注册 activity 更改为 Android.Manifest.xml 文件中的 Main/Launcher activity:

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".RegistrationActivity"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

问题:

1) 在为后续应用程序启动按下注册按钮后,如何将主 Activity 设置为启动器 activity?

public void btnRegisterPressed (View view) {

        // Save isRegistered flag into Shared Preferences
        SharedPreferences userDetails = this.getSharedPreferences("userDetails", MODE_PRIVATE);
        SharedPreferences.Editor edit = userDetails.edit();
        edit.clear();
        edit.putBoolean("isRegistered", true);

        Intent i = new Intent(this,MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
}

如果您不希望它们进入当前主页面,您将需要为该应用制作一个登陆页面并在其中包含方法 运行。所以 run 方法是进入 main 的方法。

我更愿意将我的共享偏好保留为我的应用程序的常量。

pref = getSharedPreferences(MyConstants.MY_APP);
    editor = pref.edit();



public void run(View view) {
    String getStatus = pref.getString(MyConstants.REGISTER, "nil");
    if (getStatus.equals("true")) {
        startActivity(new Intent(this, Main.class));
    } else {
        Toast.makeText(this, "Register first",
                Toast.LENGTH_SHORT).show();
    }
}

在您的注册表中 class:

editor.putString(MyConstants.REGISTER, "true");
    editor.commit();