我能够打开应用程序,然后打开登录界面,登录后会转到MainActivity
。从MainActivity
开始没有返回登录,它将一直保持在会话中直到注销。但是当我退出应用程序时,它会返回到登录屏幕,即使我有一个Sharedpref
来保存会话中的用户名。我在nohistory=true
中添加了manifest
。在MainActivity
我放置了Intent mainactivity.class
(如果按回按钮),这个原因是因为当我:
logout > login > mainactivity
然后按回按钮它将返回登出屏幕(由于myhistory=true
,登录应该没问题)。我的问题是即使应用退出,如何在会话中保留用户名。
以下链接对我没什么帮助
What is the correct way of creating a login screen/activity in Android?
Prevent showing Login screen after user login
How to presist Login credentials and do auto-login in Android
我的代码
LOGIN.java
public class Login extends Activity {
private EditText etUsername;
private Button btnLogin;
private Button btnCancel;
private TextView lblResult;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
{
this.startActivity(new Intent(Login.this,Login.class));
}
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
btnLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String username = etUsername.getText().toString();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", username);
if(username.equals("1111")){
lblResult.setText("Login successful.");
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
Logout.java
public class LogoutActivity extends Activity {
private Button btnLogout;
private Button btnCancel;
private TextView lblResult;
private EditText code;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
{
this.startActivity(new Intent(LogoutActivity.this,MainActivity.class));
}
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logout);
code = (EditText)findViewById(R.id.codeout);
btnLogout = (Button) findViewById(R.id.submit);
btnCancel = (Button) findViewById(R.id.cancel);
lblResult = (TextView)findViewById(R.id.result);
btnLogout.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String logout = code.getText().toString();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.remove("username");
if (logout.equals("99999")){
lblResult.setText("Logout successful");
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
} else {
lblResult.setText("Logout failed");
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});
答案 0 :(得分:1)
在Login.onCreate()
中,检查SharedPref中的用户ID。如果存在,请直接转到MainActivity
。
答案 1 :(得分:0)
我在应用程序的启动画面用户注销时是否有解决方案检查,然后进入登录屏幕或主屏幕
String unique_id = loginSharedPreferences
.getString("UniqueId", "NA");
if (unique_id.equals("")) {
Intent intent = new Intent(Splace_Screen.this,
LoginActivity.class);
startActivity(intent);
} else {
Intent intent = new Intent(Splace_Screen.this,
DrawerActivity.class);
startActivity(intent);
}
finish();