我在共享首选项方面遇到问题。我让一个类用户共享了与偏好设置相关的功能,然后从其他类文件中调用该功能。共享的偏好存储正确,但我无法在注销时将其删除从导航抽屉菜单中调用.Signout。我要使用删除功能,但它不起作用。任何帮助将一如既往。 这是我的用户类,包含共享的优先功能。所有功能都可以正常工作,除了removeToken
package com.example.narmail.truck30mint.Api.models.UserModels;
import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class User {
public static SharedPreferences sharedPreferences;
Context context;
@SerializedName("token")
@Expose
private static String token;
/**
* No args constructor for use in serialization
*
*/
public User(Context context) {
super();
this.context = context;
sharedPreferences = context.getSharedPreferences("user_token",Context.MODE_PRIVATE);
}
/**
*
* @param //token
*/
/* public User(String token) {
super();
this.token = token;
}*/
public static String getToken() {
if(sharedPreferences.getString(token,null) != null){
token = sharedPreferences.getString(token,null);
};
return token;
}
public void setToken(String token) {
this.token = token;
sharedPreferences.edit().putString("token",token).apply();
}
public static void removeToken(){
if(sharedPreferences.contains("token")){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("token").commit();
// editor.commit();
// sharedPreferences.edit().remove("token").apply();
System.out.println("shared preference deleting ");
}else{
System.out.println("not contain key token ");
}
}
}
我可以在日志中看到共享优先删除。但是当我在其他活动中删除令牌后获得令牌时,令牌就在那里了。在此api文件中调用了删除令牌
public static void logOut(Context context){
// User user= new User(context);
if(User.getToken() != null){
User.removeToken();
Intent a = new Intent(context, MainActivity.class);
context.startActivity(a);
}
}
然后我在主要活动中获得令牌。这是主要活动文件
package com.example.narmail.truck30mint.Api.Activities;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.narmail.truck30mint.Api.models.UserModels.User;
import com.example.narmail.truck30mint.R;
public class MainActivity extends AppCompatActivity {
public String token = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
User user = new User(getApplicationContext());
token = user.getToken();
System.out.println("in main activity token is "+token);
if (token != null) {
Intent a = new Intent(MainActivity.this, DashboardActivity.class);
startActivity(a);
}else {
setContentView(R.layout.activity_main);
Button btnNewUser = findViewById(R.id.newUser);
Button btnExistingUser = findViewById(R.id.existingUser);
btnExistingUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent a = new Intent(MainActivity.this, LoginActivity.class);
startActivity(a);
}
});
btnNewUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(i);
}
});
}
}
答案 0 :(得分:0)
尝试一下
public static void removeToken(){
if(sharedPreferences.contains("token")){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear().commit();
System.out.println("shared preference deleting ");
}else{
System.out.println("not contain key token ");
}
}
答案 1 :(得分:0)
尝试一下:-
使用
remove()
方法删除SharedPreference
令牌。
public static void removeToken(){
if(sharedPreferences.contains("token")){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("token");
editor.clear().commit();
System.out.println("shared preference deleting ");
}else{
System.out.println("not contain key token ");
}
}
要清除
SharedPreference
,请使用:-
getSharedPreferences("user_token", 0).edit().clear().commit();
答案 2 :(得分:0)
删除SharedPreferences的方式没有任何问题,因为您正在正确地从SharedPreferences中删除单个值。
您的问题仅与removeToken()
内的一个缺失部分有关
public static void removeToken(){
if(sharedPreferences.contains("token")){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("token").commit();
token = ""; //Missing this
System.out.println("shared preference deleting ");
}else{
System.out.println("not contain key token ");
}
}
您忘记从令牌变量中删除存储的令牌值。这是必需的,因为User内的token变量是静态变量,这意味着即使User实例被破坏或创建了User的新实例,该变量也将保留。
您的getToken()
代码仅进行检查以查看令牌是否存在于您的SharedPreferences中。如果存在,它将使用新值修改令牌变量。如果不存在(删除后会发生这种情况),它将返回令牌。换句话说,它只是返回在removeToken()之前存储的旧值。
因此,您的SharedPreference值实际上已正确清除。您只是在新活动中获取旧的储值。
答案 3 :(得分:0)
注销时,请调用此方法:
public static void logoutPrefs() {
SharedPreferences.Editor editor = spreferences.edit();
editor.remove("token");
editor.commit();
}