我有一个注册 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/