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

1 Answers1

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
  • 3,348
  • 8
  • 39
  • 67