Urban Side
Urban Side

Reputation: 81

Android: sharedPreference in Fragment,not read data from Class

i have this class for SharedPref work

package com.box.a100rados.Data;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import static android.content.Context.MODE_PRIVATE;


public class UserAccessSession {

    private SharedPreferences sharedPref;
    private Editor editor;


    private static final String FILTER_RADIUS_DISTANCE      = "FILTER_RADIUS_DISTANCE";
    private static final String Name = "Name";
    private static final String Photo = "Photo";
    private static final String Rank = "Rank";
    private static final String Sid = "Sid";
    private static final String Ban = "Ban";
    private static final String Latitude = "Latitude";
    private static final String Longitude = "Longitude";

    private static final String SHARED = "UserData";
    private static UserAccessSession instance;

    public static UserAccessSession getInstance(Context context) {
        if(instance == null)
            instance = new UserAccessSession(context);
        return instance;
    }

    public UserAccessSession(Context context) {
        sharedPref = context.getSharedPreferences(SHARED, MODE_PRIVATE);
        editor = sharedPref.edit();
    }

    public void storeUserSession(UserSession session) {
        editor.putString(Name, session.getUsername());
        editor.putString(Photo, session.getPhoto_url());
        editor.putInt(Rank, session.getUser_Rank());
        editor.putString(Sid, session.getUser_id());
        editor.putBoolean(Ban, session.getUser_Ban());
        editor.putString(Latitude, session.getLatitude());
        editor.putString(Longitude, session.getLongitude());
        editor.apply();
    }

    public void clearUserSession() {
        editor.putString(Name, null);
        editor.putString(Photo, null);
        editor.putInt(Rank, 1);
        editor.putString(Sid, null);
        editor.putBoolean(Ban, false);
        editor.putString(Latitude, null);
        editor.putString(Longitude, null);
        editor.apply();
    }

    public UserSession getUserSession() {
        UserSession session = new UserSession();
        session.setUsername( sharedPref.getString(Name, null) );
        session.setPhoto_url( sharedPref.getString(Photo, null) );
        session.setUser_Rank( sharedPref.getInt(Rank, 1) );
        session.setUser_id( sharedPref.getString(Sid, "") );
        session.setUser_Ban( sharedPref.getBoolean(Ban, false) );
        session.setLatitude(sharedPref.getString(Latitude,"59.946104"));
        session.setLongitude(sharedPref.getString(Longitude,"30.306048"));
        return session;
    }

    public void setFilterDistance(float radius) {
        editor.putFloat(FILTER_RADIUS_DISTANCE, radius);
        editor.commit();
    }

    public float getFilterDistance() {
        return  sharedPref.getFloat(FILTER_RADIUS_DISTANCE, 0);
    }
}

How i call:

UserAccessSession tes = UserAccessSession.getInstance(getActivity().getApplicationContext());
                UserSession test = tes.getUserSession();

But in test.getLatitude() or test.getLongitude() i have Null value. The same sometimes happens in the main activity with getUser_Rank and getUsername or other values. What have I done wrong and how can I fix it? On the map, after re-opening, I get data from the SharedPref and the map camera moves where I need.

P.S I add data like this

UserSession userSession = new UserSession();
                UserAccessSession session = UserAccessSession.getInstance(MainActivity.this);
                String sid = User_SID;
                userSession.setUsername(UserName);
                userSession.setPhoto_url(Photo_URL);
                userSession.setUser_Rank(Rank);
                userSession.setUser_id(sid);
                session.storeUserSession(userSession);

Coordinates i add like this:

userAccess = UserAccessSession.getInstance(MainActivity.this);
                userSession = userAccess.getUserSession();
                userSession.setLatitude(String.valueOf(location.getLatitude()));
                userSession.setLongitude(String.valueOf(location.getLongitude()));
                userAccess.storeUserSession(userSession);

Thank you for help. I apologize for the possible repetition of the question. I speak English poorly and it’s hard to find information.

Upvotes: 0

Views: 57

Answers (3)

Urban Side
Urban Side

Reputation: 81

I'm stupid.

userAccess = UserAccessSession.getInstance(MainActivity.this);
                userSession = userAccess.getUserSession();
                userSession.setLatitude(String.valueOf(location.getLatitude()));
                userSession.setLongitude(String.valueOf(location.getLongitude()));
                userAccess.storeUserSession(userSession);

This call cleared existing data. Transferred coordinates to a separate method, everything worked.

Upvotes: 0

touhid udoy
touhid udoy

Reputation: 4442

I guess as you are accessing is from different sources, issues occurs.

Use the default shared preference instead, which uses a single file for storage instead different ones.

Replace this context.getSharedPreferences(SHARED, MODE_PRIVATE);

with this PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

Upvotes: 1

Reham Alatris
Reham Alatris

Reputation: 435

change set to get

    public UserSession getUserSession() {
     UserSession session = new UserSession();
    session.getUsername( sharedPref.getString(Name, null) );
    session.getPhoto_url( sharedPref.getString(Photo, null) );
    session.getUser_Rank( sharedPref.getInt(Rank, 1) );
    session.getUser_id( sharedPref.getString(Sid, "") );
    session.getUser_Ban( sharedPref.getBoolean(Ban, false) );
    session.getLatitude(sharedPref.getString(Latitude,"59.946104"));
    session.getLongitude(sharedPref.getString(Longitude,"30.306048"));
    return session;
}

Upvotes: 0

Related Questions