title: Android - Using Shared Preferences in separate class?
body: <p>I want to save data using Shared Preferences in android. But I am looking to use separate class to do this task. I have implemented that class like below,  </p>
  
  <pre><code>import android.content.Context;
  import android.content.SharedPreferences;
  
  public class SavePref {
  
      private Context context;
  
      public SavePref(Context context){
          this.context = context;
      }
  
      public void saveInt(String key, int value) {
  
          SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
          SharedPreferences.Editor editor = sharedPref.edit();
          editor.putInt(key, value);
          editor.commit();
  
      }
  
  }
  </code></pre>
  
  <p>But there is an error on <code>getActivity()</code>,</p>
  
  <pre><code>The method getActivity() is undefined for the type SavePref
  </code></pre>
  
  <p>How to solve this?<br>
  Thanks</p>