Android Sharedpreferences in Fragment

Android 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);

how to use getSharedPreferences in Fragment?

Have you tried getActivity().getSharedPreferences(...)?

Edit:
Possible duplicate of Android SharedPreferences in Fragment

SharedPreferences in a Fragment

You have an unreachable code in your onCreateView - you are returning View object in first line. You should have something like that:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_account, container, false);

if(!SharedPrefManager.getInstance(getContext()).isLoggedIn()) {
startActivity(new Intent(getContext(), LoginActivity.class));
}

textViewUsername = (TextView) view.findViewById(R.id.editTextUsername);
textViewEmail = (TextView) view.findViewById(R.id.editTextEmail);
textViewFirstName = (TextView) view.findViewById(R.id.editTextFirstName);
textViewLastName = (TextView) view.findViewById(R.id.editTextLastName);

textViewUsername.setText(SharedPrefManager.getInstance(getActivity()).getUsername());
textViewEmail.setText(SharedPrefManager.getInstance(getActivity()).getEmail());
textViewFirstName.setText(SharedPrefManager.getInstance(getActivity()).getFirstName());
textViewLastName.setText(SharedPrefManager.getInstance(getActivity()).getLastName());

return view;
}

Shared Preferences in Fragment with Kotlin

You'll know why:

If you chase inheritance of Activity class you'll find out that it inherits Context class so passing "this" has no problem,
But when you chase Fragment class inheritance (androidx.fragment.app.Fragment), you'll never find that class inherits Context, so it's not acceptable to pass "this" as Context.

Actually getContext and requireContext returns their host's context, so you need to use them instead.

requireContext: returns a nonnull Context, or throws an exception when one isn't available.

getContext: returns a nullable Context.

see more about difference between "getContext" and "requireContext".

How do I use shared preferences in a fragment on Android?

Use Shared Preferences inside a Fragment; see below.

First write in SharedPreferences:

SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor edt = pref.edit();
edt.putString("facebook_id", id);
edt.commit();

Here id is the string containing the Facebook id which you've got, and 0 indicates private_mode.

Second, to read the Facebook id stored in SharedPreference in another Fragment:

SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE);
String id = pref.getString("facebook_id", "empty");

Here empty is the default value returned if facebook_id is null inside SharedPreference.

SharedPreferences in fragment?

use this code

SharedPreferences prefs;
prefs= PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("isSignIn", "Yes");
editor.commit();


Related Topics



Leave a reply



Submit