Android的用户登录错误一旦应用程序关闭 [英] Android user login error once application closes

查看:153
本文介绍了Android的用户登录错误一旦应用程序关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的共享preferences保存凭证,工作正常,但是当应用程序被杀害要求用户重新登录。

当凭据保存在用户不应该被要求登录,如果应用程序被杀害。任何帮助将是AP preciated。

 公共类MyActivity延伸活动{
     公共静态最后弦乐preFS_NAME =MYFILE;
     私人字符串用户;
     私人字符串用户名;     @覆盖
     公共无效onRestart(){
           super.onRestart();
           的userName = NULL;
           用户=无效;           //检索preferences。
           共享preferences凭证= getShared preferences(preFS_NAME,MODE_PRIVATE);           用户= credentials.getString(用户名,NULL);
           //检查所存preferences。
           如果(用户!= NULL){
              意向J =新意图(getApplicationContext(),MainActivity.class);
              startActivity(J);
              完();           }    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        //设置默认的屏幕login.xml
        的setContentView(R.layout.layout_login_new);        //初始化从XML布局的领域。        最终按钮login_btn =(按钮)findViewById(R.id.btnLogin);        //检索preferences。
        共享preferences凭证= getShared preferences(preFS_NAME,MODE_PRIVATE);
        最后一个共享preferences.Editor编辑= credentials.edit();
        的userName = NULL;
        用户=无效;
        //检索存储的值。
        用户= credentials.getString(用户名,NULL);
        //检查所存preferences。
        如果(用户!= NULL){
            意向J =新意图(getApplicationContext(),MainActivity.class);
            j.putExtra(USER_NAME用户);
            startActivity(J);
            完();
        }        //监听器登录按钮。
            login_btn.setOnClickListener(新View.OnClickListener(){                @覆盖
                公共无效的onClick(视图v){
                    意向J =新意图(getApplicationContext(),MainActivity.class);
                    //验证用户名和密码。
                    如果(
                            (username_login.getText().toString()).equals(\"test\")&&(password_login.getText().toString()).equals(\"test\")){
                        //存储在共享preferences的凭据。
                        。用户= username_login.getText()的toString();
                        。editor.putString(用户名,username_login.getText()的toString())提交();                            j.putExtra(USER_NAME用户);
                        startActivity(J);
                        完();
                        }
                    其他{
                        Display_error_msg();
                        }
                }
            });


解决方案

使用 onResume()而不是 onRestart()来检查用户登录。

这是活动的生命周期,而不是​​Application的。即onRestart()并不意味着应用程序重新启动。

I am using SharedPreferences for saving credentials which works fine but when the application is killed the user is asked to login again.

When the credentials are saved the user should not be asked to login if the application is killed. Any help would be appreciated.

public class MyActivity extends Activity {
     public static final String PREFS_NAME = "myFile";
     private String user; 
     private String userName; 

     @Override 
     public void onRestart(){
           super.onRestart();
           userName = null;
           user=null;

           //Retrieve the preferences.
           SharedPreferences credentials = getSharedPreferences(PREFS_NAME,MODE_PRIVATE); 

           user = credentials.getString(userName, null);
           //Check for stored preferences.  
           if (user!=null) {
              Intent j = new Intent(getApplicationContext(), MainActivity.class);
              startActivity(j);
              finish(); 

           } 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setting default screen to login.xml
        setContentView(R.layout.layout_login_new);

        //Initializing the fields from XML layout.

        final Button login_btn = (Button) findViewById(R.id.btnLogin);

        //Retrieve the preferences.
        SharedPreferences credentials = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
        final SharedPreferences.Editor editor = credentials.edit(); 
        userName = null;
        user=null;


        //retrieve the stored values.
        user = credentials.getString(userName, null);
        //Check for stored preferences. 
        if (user!=null){
            Intent j = new Intent(getApplicationContext(), MainActivity.class);
            j.putExtra("user_name", user);
            startActivity(j);
            finish(); 
        }    

        //Listener for the login button.
            login_btn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent j = new Intent(getApplicationContext(), MainActivity.class);
                    // Verify the username and password.
                    if ( 
                            (username_login.getText().toString()).equals("test")&&(password_login.getText().toString()).equals("test")){
                        //Store the credentials in sharedPreferences.
                        user=username_login.getText().toString();
                        editor.putString(userName, username_login.getText().toString()).commit();

                            j.putExtra("user_name", user);
                        startActivity(j);
                        finish(); 
                        }
                    else{
                        Display_error_msg();
                        }
                }
            });

解决方案

Use onResume() instead of onRestart() to checking user login.

These are activity's life cycles, Not application's. ie, onRestart() doesn't mean app restart.

这篇关于Android的用户登录错误一旦应用程序关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆