android app重新启动后返回登录页面

时间:2016-11-29 07:54:05

标签: android xamarin.android

我正面临重新启动Android应用程序的问题,i; e

当我运行我的应用程序时,第一个活动是登录,登录时它将导航到主页。

假设如果按下主页按钮并从app-drawer重新启动应用程序,它将再次从登录页面开始。但是如果从任务管理器中的运行列表打开它,它将带有登录页面。如何解决这个问题请帮助我。

1 个答案:

答案 0 :(得分:0)

将此代码添加到 Login.java OnCreate

if (!TextUtils.isEmpty(SessionManager.isLogIn(Login.this))) {

        SessionManager.user_id = SessionManager.isLogIn(Login.this);
        SP = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SessionManager.user_name = SP.getString("user_name", "");
        SessionManager.user_name = SP.getString("user_pass", "");

        SessionManager.uId = SP.getString("id", "");
        Intent intent = new Intent(Login.this, HomePage.class);
        startActivity(intent);
        finish();
    }

将java类命名为 SessionManager.java

public class SessionManager {

    static Context context;

    public static String PREFS_NAME = "settings";
    static SharedPreferences preferences;

    public static String user_id ="";
    public static String user_name ="";

    public static String user_pass ="";

    public static boolean check = true;


  @Override
  public void onCreate() {
      super.onCreate();
      context = getApplicationContext();
      mInstance = this;
  }


  public static String isLogIn(Context context) {
      preferences = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

      user_name = preferences.getString("user_name", "");
      user_pass = preferences.getString("user_pass", "");

      return user_id = preferences.getString("user_id", "");
  }

  public boolean logoutUser(Context context) {
      preferences = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);


      return true;
  }

}

Login.java

onClick LOGIN添加此代码
    SP = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        Editor editit = SP.edit();
        editit.putString("user_id", user_id);
        editit.putString("user_name", etUsername.getText().toString());
        editit.putString("user_pass", etPassword.getText().toString());

        editit.commit();

        SessionManager.user_id = user_id;
        SessionManager.user_name = etUsername.getText().toString());
        SessionManager.user_pass = etPassword.getText().toString());

Login.java

中设置此Global
    SharedPreferences SP;