title: Android - save Object to SharedPreferences and get it anywhere in the app
body: <p>In my app I have a custom <code>User</code> class which holds some regular data (name etc...). I need to save that object and get it anywhere and anytime in other pages of the app. I made a helper class <code>public final class GeneralMethods</code> with many methods which I use a lot (static, of course). <br> In order to save the data Im using <code>Gson</code> library. I made this method:<br></p>
  
  <pre><code>public static void saveData(Context con, String variable, String data)
  {
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(con);
      prefs.edit().putString(variable, data).apply();
  }
  </code></pre>
  
  <p>To save an object, I use this method as follows:<br></p>
  
  <pre><code>Gson gson = new Gson();
  String stringUser = gson.toJson(newUser);    
  GeneralMethods.saveData(VerificationActivity.this,"userObject",stringUser);
  </code></pre>
  
  <p>To load the data back, I'm using this static method:<br></p>
  
  <pre><code>public static String getData(Context con, String variable, String defaultValue)
  {
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(con);
      String data = prefs.getString(variable, defaultValue);
      return data;
  }
  </code></pre>
  
  <p>I dont really know how to get the data back, this is what I've done so far:</p>
  
  <pre><code>Gson gson = new Gson();
  String user="";
  String value="";
  user = GeneralMethods.getData(SplashScreenActivity.this,value,"userObject");
  </code></pre>
  
  <p>Im struggling with the <code>getData</code> method, how do I parse the data from <code>String</code> back to the <code>User</code> type?</p>
  
  <p><br>EDIT<br>
  I tried the suggestions bellow and I always get <code>NULL</code>. Maybe I dont save the object in the right way?</p>
  
  <p><br>EDIT2<br>
  It seems Im not generating the object correctly and therefore nothing is being saved. This is the user "Singleton" class:<br></p>
  
  <pre><code>public class User implements Serializable {
  
      private static User userInstance=null; //the only instance of the class
      private static String userName; //userName = the short phone number
      private User(){}
  
      public static User getInstance(){
          if(userInstance ==null){
              userInstance = new User();
          }
          return userInstance;
      }
  
      public static User getUserInstance() {
          return userInstance;
      }
  
      public String getUserName(){
          return this.userName;
      }
  
      public static void setUserName(String userName) {
          User.userName = userName;
      }
  
      public static void init(String _userName) {
          User.setUserName(_userName);
      }
  }
  </code></pre>
  
  <p>This is how i setup the object with the relevant data (user name as the constructor parameter):</p>
  
  <pre><code>   User.init(name);
  </code></pre>
  
  <p>This is how i convert the object to a <code>String</code>:</p>
  
  <pre><code>   Gson gson = new Gson();
      String stringUser = gson.toJson(User.getInstance());
  GeneralMethods.saveData(VerificationActivity.this,"userObject",stringUser);
  </code></pre>