0

I am working on a app and while getting my shared preferences(which are saved in login activity), i am trying to get it in dashboard fragment but i am not be able to get it. After this i checked whether the is saved or not so then i used

boolean ok= editor.commit();
Toast.makeText(Login.this, "Saved: "+ok, Toast.LENGTH_LONG).show();

My toast shows message as Saved:true

After this try i am assuming that my data is saved to preferecnces but i am unable to fetch it. Below is my dashboard fragmenr code.

public class dashboard extends Fragment {
private TextView comp_text,mail_text,gst_text;
private String mUsername;
private SharedPreferences mSharedPreferences;



@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //this inflates out tab layout file.
    View x = inflater.inflate(R.layout.dashboard_frag, null);
    comp_text=(TextView)x.findViewById(R.id.company_id);
    mail_text=(TextView)x.findViewById(R.id.email_id);
    gst_text= (TextView)x.findViewById(R.id.gst_id);
    initSharedPreferences();
    Toast.makeText(getActivity(), "Logged member->  "+mUsername, Toast.LENGTH_LONG).show();
    return x;

}
private void initSharedPreferences() {
     mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    mUsername = mSharedPreferences.getString(Constants.USERNAME, "");

    }
}

Here my Toast show **logged member-> **, that means musername have nothing to print and preferences are unable get.

I'm still confused this is my point of view if you want i can show where i saved preferences.

Help will be appreciated ! THANKS !

EDIT 1 ---- Here is my onResponse function where i saved preferences.

 public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
            if(response.isSuccessful()) {
                ServerResponse serverResponse = response.body();
                if(serverResponse.getMessage().equals(username)) {
                    SharedPreferences.Editor editor = mSharedPreferences.edit();
                    editor.putBoolean("LoggedIn",true);
                    editor.putString(Constants.USERNAME,serverResponse.getMessage());

                   boolean ok= editor.commit();


                    Toast.makeText(Login.this, "Saved: "+ok, Toast.LENGTH_LONG).show();
                    goToProfile();
                }
            } else {
                Gson gson = new Gson();
                ServerResponse errorResponse = null;
                try {
                    errorResponse = gson.fromJson(response.errorBody().string(), ServerResponse.class);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Snackbar.make(loginButton,errorResponse.getMessage(),Snackbar.LENGTH_SHORT).show();
            }
        }

2 Answers2

2

Solution:

Here is the simple example of storing and retrieving Shared Preferences

Setting values in Preference:

// MY_PREFS_NAME - a static String variable like: 
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Elena");
 editor.putInt("idName", 12);
 editor.commit();

Retrieve data from preference:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  int idName = prefs.getInt("idName", 0); //0 is the default value.
}

more info:

Using Shared Preferences

Shared Preferences

Source: Shared Preferences Simple Example

In your case, you might want to replace your code as shown below:

Try this

SharedPreferences.Editor editor = getSharedPreferences("my_prefs", MODE_PRIVATE).edit();
editor.putBoolean("LoggedIn",true);
editor.putString(Constants.USERNAME,serverResponse.getMessage());
boolean ok= editor.commit();

And then in Fragment

mSharedPreferences = getActivity().getSharedPreferences("prefs", MODE_PRIVATE); ;
mUsername = mSharedPreferences.getString(Constants.USERNAME, "");

If you want to logout and remove the user login, just clear the SharedPreferences:

SharedPreference.Editor pref = context.getSharedPreferences("prefs", MODE_PRIVATE).edit();
pref.clear();
pref.commit();

Hope this helps.

Ümañg ßürmån
  • 8,143
  • 4
  • 21
  • 40
1

Try this

SharedPreferences.Editor editor = getSharedPreferences("my_prefs", MODE_PRIVATE).edit();;
                    editor.putBoolean("LoggedIn",true);
                    editor.putString(Constants.USERNAME,serverResponse.getMessage());

                   boolean ok= editor.commit();

And then in Fragment

mSharedPreferences = getActivity().getSharedPreferences("my_prefs", MODE_PRIVATE); ;
    mUsername = mSharedPreferences.getString(Constants.USERNAME, "");
theanilpaudel
  • 2,840
  • 8
  • 28
  • 55