我有两个活动,即注册和登录,在注册类中定义了共享首选项。在“注册”页面上,如果单击使您从“注册活动”进入“登录活动”的按钮。我正在存储“共享首选项”值,以便在第二次启动该应用程序时,让该应用程序立即转到LoginActivity,而不是打开第一个活动(即注册活动)。这是我称为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。请协助

最佳答案

尝试这个

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){

        }

关于java - 共享首选项未获取值(value),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47469551/

10-10 16:13