问题描述
我正在使用 SharedPrefernces 来获取用户/注销用户,但我一直收到错误“从活动上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志”。注销方法用于清除设备中存储的数据。
logout_nav.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPrefManager.getInstance(getApplicationContext()).logout();
}
我的共享偏好代码是
public class SharedPrefManager {
private static final String SHARED_PREF_NAME = "volleyregisterlogin";
private static final String KEY_PHONENUMBER = "keyphonenumber";
private static final String KEY_LASTNAME = "keylastname";
private static final String KEY_FULLNAME = "keyfullname";
private static final String KEY_MIDDLEINITIAL = "keymiddleinitial";
private static final String KEY_COUNTRY = "keycountry";
private static final String KEY_ADDRESS = "keyaddress";;
private static final String KEY_ID = "keyid";
private static final String KEY_BALANCE = "keybalance";
private static SharedPrefManager mInstance;
private static Context ctx;
private SharedPrefManager(Context context) {
ctx = context;
}
public static synchronized SharedPrefManager getInstance(Context context) {
if (mInstance == null) {
mInstance = new SharedPrefManager(context);
}
return mInstance;
}
//this method will store the user data in shared preferences
public void userLogin(User user) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(KEY_PHONENUMBER,user.getPhone_number());
editor.putString(KEY_LASTNAME,user.getLastname());
editor.putString(KEY_FULLNAME,user.getFullname());
editor.putString(KEY_MIDDLEINITIAL,user.getMiddleinitial());
editor.putString(KEY_COUNTRY,user.getCountry());
editor.putString(KEY_ADDRESS,user.getAddress());
editor.putFloat(KEY_BALANCE,(float) user.getBalance());
editor.apply();
}
//this method will checker whether user is already logged in or not
public boolean isLoggedIn() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME,Context.MODE_PRIVATE);
return sharedPreferences.getString(KEY_PHONENUMBER,null) != null;
}
//this method will give the logged in user
public User getUser() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME,Context.MODE_PRIVATE);
return new User(
sharedPreferences.getString(KEY_PHONENUMBER,null),sharedPreferences.getString(KEY_LASTNAME,sharedPreferences.getString(KEY_FULLNAME,sharedPreferences.getString(KEY_MIDDLEINITIAL,sharedPreferences.getString(KEY_COUNTRY,sharedPreferences.getString(KEY_ADDRESS,sharedPreferences.getFloat(KEY_BALANCE,0)
);
}
//this method will logout the user
public void logout() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
ctx.startActivity(new Intent(ctx.getApplicationContext(),Login.class));
}
}
解决方法
您传递给 SharedPrefManager 的 ctx 是 ApplicationContext ,它不允许启动 Activity 。所以
logout_nav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPrefManager.getInstance(xxxActivity.this).logout();
}
会很好。将 xxxActivity 更改为您自己的活动。