0

我的应用程序出现了一些问题,登录后它会进入主页。在主页中,应用程序在后台被注销。下次我要打开应用程序时,它会从登录页面重新启动。

我已经传递了值并设置了通知,我不知道如何防止它可以帮助我@Thanks

4

2 回答 2

2
  • 将登录详细信息保存在SharedPreferences.

将登录详细信息保存到 SharedPreference

SharedPreferences sp = context.getSharedPreferences("loginSaved", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("username", "some user value");
editor.putString("password", "some password value");
editor.commit();
  • SharedPreference检查打开登录页面是否有价值。
  • 如果登录详细信息保存在 SharedPreference 中,请转到主页。否则显示登录页面。

检查是否登录

SharedPreferences sp = context.getSharedPreferences("loginSaved", Context.MODE_PRIVATE);
String username = sp.getString("username", null);
String password = sp.getString("password", null);
if(username != null && password != null){
    // login automatically with username and password    
}
else{
    // show login page
}
  • SharedPreference从用户正确注销时清除 loginDetails 。
于 2013-05-06T10:25:03.337 回答
0

I assume your activity flow is as follows: Login -> Main. You could reverse the activities so that the main activity launches login activity as needed.

  • Make your main activity also the MAIN activity in manifest so it is the default.

  • In each activity requiring logged-in state, check for valid session or credentials in onResume() and launch the login activity if needed. To prevent a loop in login activity back press -> main activity resumed -> login activity launched, you can for example override login activity's onBackPressed() to call moveTaskToBack(true) to move your app to background.

  • When login is successful, just finish() the login activity so that the previous activity such as your main activity is resumed.

  • Explicit logout should clear the session/credentials data and probably launch the login activity.

This will maintain your application's back stack in a consistent state and also easily extends to multiple session-requiring activities the app can be resumed to.

To store credential/session data locally, use e.g. SharedPreferences.

于 2013-05-06T11:07:58.890 回答