【发布时间】:2021-03-16 09:25:58
【问题描述】:
我正在使用 SharedPrefernces 获取用户/注销用户,但我不断收到错误消息“从 Activity 上下文之外调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志”。 logout方法用于清除设备中存储的数据。
这是我用于注销方法的:
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, null),
sharedPreferences.getString(KEY_FULLNAME, null),
sharedPreferences.getString(KEY_MIDDLEINITIAL, null),
sharedPreferences.getString(KEY_COUNTRY, null),
sharedPreferences.getString(KEY_ADDRESS, null),
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));
}
}
【问题讨论】:
-
您的问题与
SharedPreferences无关。标题具有误导性。您的问题是您在非Activity上下文中调用startActivity()(在这种情况下可能是ApplicationContext。