更改AndroidManifest.xml以根据共享首选项设置LAUNCHER活动

时间:2015-09-17 02:47:07

标签: android android-manifest

我有注册活动,也有主要活动。

我想要实现的目标:

我想:

1)如果用户尚未注册,则显示注册活动

2)显示用户已注册的主要活动

我做了什么:

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

    <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)如何在按下注册按钮后启动后续应用程序时,如何将主活动设置为Launcher活动?

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);
}

1 个答案:

答案 0 :(得分:1)

如果您不希望它们落入当前主网页,您需要为该应用创建一个目标网页并在其中运行该方法。因此,run方法是主要方法。

我更喜欢将我的共享偏好设置为我的应用的常量。

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

在您的注册类中:

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