title: Save data from fragment to custom session
body: <p>I am creating a project in which I am using navigation drawer and on navigation item click it opens a login page and after login it movies to my main activity. Now, I want to save my login details and all data in my custom shared preference from the fragment. After that it on app restarts and I go to navigation login items it should check if the user is already logged in then it should move to an activity otherwise it should run my login fragment</p>
  
  <p>my login fragment </p>
  
  <pre><code>public View onCreateView(@NonNull LayoutInflater inflater,
                           ViewGroup container, Bundle savedInstanceState) {
  
      CDSMainViewModel =
          ViewModelProviders.of(this).get(CDP_MainViewModel.class);
      View root = inflater.inflate(R.layout.fragment_cdplogin, container, false);
  
      context = container.getContext();
      context = getActivity().getApplicationContext();
      session = new Session(context);
  
  
      if (session.isLoggedIn()) {
          Intent itra = new Intent(getActivity().getApplicationContext(), Verification_Activity.class);
          startActivity(itra);
          getActivity().finish();
      }
      else{
          login_button = root.findViewById(R.id.button_click_login);
          edit1 = root.findViewById(R.id.input_username);
          edit2 = root.findViewById(R.id.input_password);
          layout_1 = root.findViewById(R.id.usnamelayout);
          layout_2 = root.findViewById(R.id.password_layout);
  
  
          login_button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
  
              }
          });
      }
  
      return root;
  }
  
  private void Login_data() {
  
      String URL = "http://117.240.196.238:8080/api/cdp/getAuth";
      Log.i("response", URL);
      StringRequest jsonObjRequest = new StringRequest(
          Request.Method.POST, URL, new Response.Listener&lt;String&gt;() {
              @Override
              public void onResponse(String response) {
                  Log.i("response_login", response);
  
                  parseData1(response);
  
  
              }
          },
          new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                  VolleyLog.d("volley", "Error: " + error.getMessage());
                  showServerConnectionError();
              }
          }) {
  
          @Override
          public String getBodyContentType() {
              return "application/x-www-form-urlencoded; charset=UTF-8";
          }
  
          @Override
          protected Map&lt;String, String&gt; getParams() throws AuthFailureError {
              Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
              params.put("AuthID", name);
              params.put("AuthPwd", password);
  
              return params;
          }
  
      };
      RequestQueue queue = SingletonRequestQueue.getInstance(getActivity().getApplicationContext()).getRequestQueue();
      queue.add(jsonObjRequest);
  }
  private void parseData1(String response){
      try {
          JSONObject json = new JSONObject(response);
          int success = json.getInt("success");
          String msg = json.getString("message");
          if(success == 1){
  
              Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
              JSONArray recData = json.getJSONArray("data");
              for (int i = 0; i &lt; recData.length(); i++) {
  
                  JSONObject c = recData.getJSONObject(i);
  
                  String uname = name;
                  String upass = password;
  
                  String emp_id  = c.getString("emp_id");
                  String empname = c.getString("EmpName");
                  String office = c.getString("OfficeNameFull");
                  String desig = c.getString("DesignationName");
                  String mobile = c.getString("EmpMobile");
                  String office_code = c.getString("OfficeCode");
                  String OfficeType = c.getString("OfficeType");
                  String district = c.getString("DistrictCode");
                  String DistrictName = c.getString("DistrictName");
                  String designationCode = c.getString("designationCode");
                  String DesignationName1 = c.getString("DesignationName1");
                  String DesigShortName = c.getString("DesigShortName");
  
                  Log.i("session",district);
  
                  //Here my loginsession is not working its not saving my data in my session..  
                  Session.getInstance(getContext()).loginsession(emp_id,uname,upass,empname,office,desig,mobile,office_code,OfficeType,district,DistrictName,designationCode,DesignationName1,DesigShortName);
                  //  String email = SessionManager.getInstance(context).getUserEmail();
                  //   session.loginsession(emp_id,uname,upass,empname,office,desig,mobile,office_code,OfficeType,district,DistrictName,designationCode,DesignationName1,DesigShortName);
  
                  Intent its12 = new Intent(getActivity().getApplicationContext(), Verification_Activity.class);
  
                  startActivity(its12);
                  getActivity().overridePendingTransition(R.anim.from_right, R.anim.slide_to_left);
                  getActivity().finish();
              }
  
  
          } else {
              Toast.makeText(context,msg, Toast.LENGTH_LONG).show();
          }
      }catch(Exception ex){
      }
  }
  }
  </code></pre>
  
  <p>//Session manager class</p>
  
  <pre><code>package com.example.agridept.domain;
  
  import android.content.Context;
  import android.content.Intent;
  import android.content.SharedPreferences;
  
  import com.example.agridept.ui.CDP_login.CDP_MainFragment;
  
  public class Session {
  
      private static Session jInstance;
      SharedPreferences preferences;
      SharedPreferences.Editor editor;
      Context context;
      int PRIVATE_MODE = 0;
  
      private static final String IS_LOGIN = "IsLoggedIn";
      private static final String PREF_NAME = "AndroidHivePref";
  
      String emp_id1,username1,password1 ,empname1 , office1 , desig1, mobile1, office_code1, OfficeType1 , district1 ,DistrictName1 , designationCode1, DesignationName11 , DesigShortName1;
  
  
      public Session(Context context){
          this.context = context;
          preferences = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
          editor = preferences.edit();
  
      }
  
  public void loginsession(String emp_id,String username,String password11 ,String empname ,String  office ,String  desig,String  mobile,String  office_code,String  OfficeType ,String  district ,String DistrictName ,String  designationCode,String  DesignationName1 ,String  DesigShortName){
      editor.putBoolean(IS_LOGIN, true);
      editor.putString(emp_id1,emp_id);
      editor.putString(username1,username);
      editor.putString(password1,password11);
      editor.putString(empname1,empname);
      editor.putString(office1,office);
      editor.putString(desig1,desig);
      editor.putString(mobile1,mobile);
      editor.putString(office_code1,office_code);
      editor.putString(OfficeType1,OfficeType);
      editor.putString(district1,district);
      editor.putString(DistrictName1,DistrictName);
      editor.putString(designationCode1,designationCode);
      editor.putString(DesignationName11,DesignationName1);
      editor.putString(DesigShortName1,DesigShortName);
  }
  
      public boolean isLoggedIn() {
          return preferences.getBoolean(IS_LOGIN, false);
      }
  
  
      public void logoutUser() {
          // Clearing all data from Shared Preferences
          editor.clear();
          editor.commit();
          // After logout redirect user to Loing Activity
          Intent i = new Intent(context, CDP_MainFragment.class);
          // Closing all the Activities
          i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          // Add new Flag to start new Activity
          i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          // Staring Login Activity
          context.startActivity(i);
      }
  
      public void clearinfo() {
          // Clearing all data from Shared Preferences
          editor.clear();
          editor.commit();
      }
  
      public SharedPreferences getPreferences() {
          return preferences;
      }
  
      public void setPreferences(SharedPreferences preferences) {
          this.preferences = preferences;
      }
  
      public SharedPreferences.Editor getEditor() {
          return editor;
      }
  
      public void setEditor(SharedPreferences.Editor editor) {
          this.editor = editor;
      }
  
      public Context getContext() {
          return context;
      }
  
      public void setContext(Context context) {
          this.context = context;
      }
  
      public int getPRIVATE_MODE() {
          return PRIVATE_MODE;
      }
  
      public void setPRIVATE_MODE(int PRIVATE_MODE) {
          this.PRIVATE_MODE = PRIVATE_MODE;
      }
  
      public static String getIsLogin() {
          return IS_LOGIN;
      }
  
      public static String getPrefName() {
          return PREF_NAME;
      }
  
      public String getEmp_id1() {
          return emp_id1;
      }
  
      public void setEmp_id1(String emp_id1) {
          this.emp_id1 = emp_id1;
      }
  
      public String getPassword1() {
          return password1;
      }
  
      public void setPassword1(String password1) {
          this.password1 = password1;
      }
  
      public String getEmpname1() {
          return empname1;
      }
  
      public void setEmpname1(String empname1) {
          this.empname1 = empname1;
      }
  
      public String getOffice1() {
          return office1;
      }
  
      public void setOffice1(String office1) {
          this.office1 = office1;
      }
  
      public String getDesig1() {
          return desig1;
      }
  
      public void setDesig1(String desig1) {
          this.desig1 = desig1;
      }
  
      public String getMobile1() {
          return mobile1;
      }
  
      public void setMobile1(String mobile1) {
          this.mobile1 = mobile1;
      }
  
      public String getOffice_code1() {
          return office_code1;
      }
  
      public void setOffice_code1(String office_code1) {
          this.office_code1 = office_code1;
      }
  
      public String getOfficeType1() {
          return OfficeType1;
      }
  
      public void setOfficeType1(String officeType1) {
          OfficeType1 = officeType1;
      }
  
      public String getDistrict1() {
          return district1;
      }
  
      public void setDistrict1(String district1) {
          this.district1 = district1;
      }
  
      public String getDistrictName1() {
          return DistrictName1;
      }
  
      public void setDistrictName1(String districtName1) {
          DistrictName1 = districtName1;
      }
  
      public String getDesignationCode1() {
          return designationCode1;
      }
  
      public void setDesignationCode1(String designationCode1) {
          this.designationCode1 = designationCode1;
      }
  
      public String getDesignationName11() {
          return DesignationName11;
      }
  
      public void setDesignationName11(String designationName11) {
          DesignationName11 = designationName11;
      }
  
      public String getDesigShortName1() {
          return DesigShortName1;
      }
  
      public void setDesigShortName1(String desigShortName1) {
          DesigShortName1 = desigShortName1;
      }
  
      public static synchronized Session getInstance(Context context) {
          if (jInstance != null) {
              return jInstance;
          } else {
              jInstance = new Session(context);
              return jInstance;
          }
      }
  }
  </code></pre>