片段中的 SharedPreferences

SharedPreferences in a Fragment

我正在尝试在片段中实现 SharedPreferences。我正在尝试显示登录用户的详细信息。但是,我无法在帐户片段上显示它。在 MainActivity 中显示 SharedPreferences 时没有问题。我正在寻找一种解决方案来解决如何满足代码的需求,以便它可以在片段中工作。

MainActivity.java

if(!SharedPrefManager.getInstance(this).isLoggedIn()){
            finish();
            startActivity(new Intent(this, LoginActivity.class));
        }
        textviewUsername = (TextView)findViewById(R.id.usernameLabel);
        textviewUsername.setText(SharedPrefManager.getInstance(this).getUsername());

在这个 AccountFragment 中,我试图显示帐户详细信息。

AccountFragment.java

public class AccountFragment extends Fragment {
private TextView textViewUsername, textViewEmail, textViewFirstName, textViewLastName;

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

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

        textViewUsername = (TextView) getView().findViewById(R.id.editTextUsername);
        textViewEmail = (TextView) getView().findViewById(R.id.editTextEmail);
        textViewFirstName = (TextView) getView().findViewById(R.id.editTextFirstName);
        textViewLastName = (TextView) getView().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());

}

您的 onCreateView 中有一段无法访问的代码 - 您在第一行返回了 View 对象。你应该有这样的东西:

@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;
}

好吧,这可能有用,因为它对我有用。我将它保存在片段中:

getActivity().getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
                                .edit()
                                .putString(PREF_EMAIL, email)
                                .putString(PREF_PASSWORD, password)
                                .apply();

然后我让它们进入主活动:

SharedPreferences pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    String email = pref.getString(PREF_EMAIL, null);
    String password = pref.getString(PREF_PASSWORD, null);

我不明白为什么它不能反向工作。

我建议使用默认的共享首选项,除非您确实需要这样做:

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = sharedPrefs.edit();

editor.putString("key", "value");
editor.apply();

然后检索值:

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
String storedValue = sharedPrefs.getString("key", "defaultValue");