Jian Ruiz Mangampo
Jian Ruiz Mangampo

Reputation: 189

calling startActivity() requiring FLAG_ACTIVITY_NEW_TASK

I'm using SharedPrefernces for getting the user/logging out the user but I keep getting the error "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag". the logout method is used to clear the stored data from the device.

This is what i'm using for the logout method:

logout_nav.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPrefManager.getInstance(getApplicationContext()).logout();
        }

and my shared preference code is

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

   }
}

Upvotes: 0

Views: 63

Answers (1)

cowboi-peng
cowboi-peng

Reputation: 803

the ctx you passed to SharedPrefManager is a ApplicationContext , it won't allowed to start a Activity . so

logout_nav.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPrefManager.getInstance(xxxActivity.this).logout();
        }

would be nice . change xxxActivity to your own activity.

Upvotes: 1

Related Questions