android - 更改 AndroidManifest.xml 以根据共享首选项设置 LAUNCHER Activity

标签 android android-manifest

我有一个注册 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();
    }
}

在你的注册类中:

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

关于android - 更改 AndroidManifest.xml 以根据共享首选项设置 LAUNCHER Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32621835/

相关文章:

安卓多点触控!破解任何人?

android - 使我的应用不适用于平板电脑

安卓 "Not granting permission"错误

Android 添加某些权限后无法调试

java - 连接丢失后 Firebase Firestore 需要很长时间才能重新连接

javascript - 了解闭包编译器警告

android - 将 APK 上传到 Google Play 时出错

android - 如果从超链接打开应用程序启动两次

java - 对话框导致应用程序崩溃

android - 在所有 Activity 中访问 GoogleApiClient 对象