Java 仅显示一次登录活动,并在下一次启动主活动时显示
我想在第一次进入应用程序时打开login_活动,然后在第二次进入应用程序时打开main_活动 我创造了一些东西,但它不起作用。所以我想知道我做错了什么? 这是我的后勤工作Java 仅显示一次登录活动,并在下一次启动主活动时显示,java,android,Java,Android,我想在第一次进入应用程序时打开login_活动,然后在第二次进入应用程序时打开main_活动 我创造了一些东西,但它不起作用。所以我想知道我做错了什么? 这是我的后勤工作 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); userName
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userName = (EditText) findViewById(R.id.username);
userPhone = (EditText) findViewById(R.id.userPhone);
loginBtn = (Button) findViewById(R.id.buttonLogin);
dbHandler = new LogsDBHandler(this);
loginBtn.setOnClickListener(this);
setTitle("AMS - biomasa | prijava");
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
if (pref.getBoolean("activity_executed", false)) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
} else {
SharedPreferences.Editor edt = pref.edit();
edt.putBoolean("activity_executed", true);
edt.commit();
}
}
public void insert() {
User user = new User (
userName.getText().toString(),
userPhone.getText().toString());
dbHandler.addUser(user);
Toast.makeText(getBaseContext(), "Prijavljeni ste!", Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
if (v == loginBtn && validateUser()) {
insert();
}
}
在主活动中,我只有图像和两个按钮。
在清单中,我将启动器添加到主活动和登录活动中
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我做错了什么 您应该添加另一个空活动(没有UI),该活动在加载之前加载 然后使用
SharedReferences
存储一些值。因此,如果用户已经打开了您的应用程序一次,则会存储该值。然后使用条件检查此值。如果是您保存的值,则跳过login\u活动
并直接转到main\u活动
,否则直接转到login\u活动
public class SplashActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// decide here whether to navigate to Login or Main Activity
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
if (pref.getBoolean("activity_executed", false)) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
} else {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
}
}
活动设置为true
public void insert() {
User user = new User (
userName.getText().toString(),
userPhone.getText().toString());
dbHandler.addUser(user);
Toast.makeText(getBaseContext(), "Prijavljeni ste!", Toast.LENGTH_SHORT).show();
//set activity_executed inside insert() method.
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
SharedPreferences.Editor edt = pref.edit();
edt.putBoolean("activity_executed", true);
edt.commit();
}
<activity android:name=".MainActivity"/>
<activity android:name=".LoginActivity" />
<activity android:name=".SplashActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
线路问题
if (pref.getBoolean("activity_executed", false)) {
您可以实现此方法来调用内部if(appIsLoggedIn)
您可以将启动器活动更改为main activity。这样,当您打开应用程序时,它将从main activity开始,您可以检查他是否已登录。如果他未登录,您必须将他导航到登录活动,否则您只能按原样操作。以下是清单文件
use SharedPreference可能重复…在其中设置成功登录的标志,下次每次检查标志和加载活动时,都应相应地删除mainActivity中的启动器,我也有疑问,即使用户没有单击登录,您也会在值上指定true,但这是一个细节。。无论如何,kevz的答案是最好的,我认为这是好的,但不知何故,当我进入应用程序时,它总是打开我的登录活动。你可能知道这是为什么吗?或者我做错了。@RubyDigger19:启动程序“意图过滤器”的第一个活动已启动。在您的情况下,它应该是MainActivity,而不是LoginActivity。序列正确吗,即清单文件中的MainActivity和LoginActivity?我照你说的做了。即使我先打开MainActivity,它也不会在LoginActivity之前打开。但是当LOginActivity是第一个时,每次我进入应用程序时,它都会打开它。@RubyDigger19:你想在聊天中谈论它吗?这里是链接
public boolean appIsLoggedIn(){
return pref.getBoolean("activity_executed", false);
}