【问题标题】:Shared Preferences not retrieving value共享首选项未检索值
【发布时间】:2017-11-24 08:53:42
【问题描述】:

我有两个活动,即注册和登录,我在注册类中定义了一个共享首选项。在注册页面上,如果您单击将您从注册活动带到登录活动的按钮。我正在存储一个共享首选项值,以便在第二次启动应用程序时,让应用程序立即进入 LoginActivity,而不是打开第一个 Activity,即注册 Activity。这是我的名为 Session 的 SharedPreferences 类。

private SharedPreferences prefs;

    public Session(Context cntx) {
        // TODO Auto-generated constructor stub
        prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
    }

    public void setusename(String usename) {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("username", usename).commit();
        editor.commit();

public Boolean contKey(String key){
        if (prefs.contains(key)){
            return true;
        }else{
           return false;
        }
    }

这是 RegstrationActivity 中的按钮,用于在转到 LoginActivity 之前存储 SharedPreferences 值,以便在第二次启动时打开 LoginActivity 类

loginLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                session = new Session(getApplicationContext());
                session.setusename("hello123");

                Intent intent = new Intent(getApplication(),ActivityLogin.class);
                startActivity(intent);
                finish();
            }
        });

我在 RegistrationActivity 类的 onCreate 方法中定义了这个,以便在下次启动应用时切换到 ActivityLogin 屏幕

System.out.println("it got here...2");
        try {
            if (session.contKey("username")) {
                System.out.println("it got here...");
                Intent intent = new Intent(getApplication(), ActivityLogin.class);
                startActivity(intent);
                overridePendingTransition(R.anim.enter, R.anim.exit);
                finish();
            } else {
                System.out.println("it got here...3");
                Intent intent = new Intent(getApplication(), ActivityRegistration.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.enter, R.anim.exit);
            }
        }catch(Exception ex){

        }

请问为什么我的共享首选项在第二次启动时没有切换到 LoginActivity。请帮忙

【问题讨论】:

    标签: java android


    【解决方案1】:

    试试这个

    private SharedPreferences prefs;
    
        public Session(Context cntx) {
            // TODO Auto-generated constructor stub
            prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
        }
    
        public void setusename(String usename) {
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("username", usename);
            editor.commit();
    
            }
    
       }
    
        public String getusename() {
           return prefs.getString("username","");    
         }
      }
    

    比用这种方式获取价值SharedPreferences

    session = new Session(getApplicationContext());
    session.setusename("hello123");
    String username=session.getusename();
    

    示例代码

    try {
                if (!session.getusename().equals("")) {
                    System.out.println("it got here...");
                    Intent intent = new Intent(getApplication(), ActivityLogin.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.enter, R.anim.exit);
                    finish();
                } else {
                    System.out.println("it got here...3");
                    Intent intent = new Intent(getApplication(), ActivityRegistration.class);
                    startActivity(intent);
                    finish();
                    overridePendingTransition(R.anim.enter, R.anim.exit);
                }
            }catch(Exception ex){
    
            }
    

    【讨论】:

    • 如何在打开第二个活动时验证 if 语句
    • if语句检查中使用username键怎么样
    • @Smith 没找到你
    • 这样做 >>>> if (session.contKey("username"))
    • @Smith 可能你可以试试,但用我的方式
    【解决方案2】:

    您可以在Activity A中定义一个具有您想要的名称的私有首选项,而不是默认的共享首选项..如下所示..

    SharedPreferences preferences = getSharedPreferences("myPreferences",0);
            SharedPreferences.Editor editor= preferences.edit();
            editor.putString("userName","raju");
            editor.commit();
    

    如果您想从 Activty A(我的意思是在同一个活动中)中的共享偏好中检索该值,您可以使用以下代码..

    String user_name= preferences.getString("userName","N/A");
    

    否则在Activity B中(我的意思是在其他一些activity中),这次你不需要编辑器(用于检索)..代码如下

    SharedPreferences preferences = getSharedPreferences("myPreferences",0);
    String user_name= preferences.getString("userName","N/A");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 2012-09-10
      • 2020-10-04
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多