Skip to content

Instantly share code, notes, and snippets.

@gblnovaes
Created August 3, 2016 19:55
Show Gist options
  • Save gblnovaes/45c807e82242b61a8f268fed0ae05355 to your computer and use it in GitHub Desktop.
Save gblnovaes/45c807e82242b61a8f268fed0ae05355 to your computer and use it in GitHub Desktop.
SharedPreferences in Fragment
The method getSharedPreferences is a method of the Context object, so just calling getSharedPreferences from a Fragment will not work...because it is not a Context! (Activity is an extension of Context, so we can call getSharedPreferences from it).
So you have to get your applications Context by
// this = your fragment
SharedPreferences preferences = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
@yeray-yas
Copy link

Thanks a lot, dude!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment