片段中的SharedPreferences

时间:2017-04-19 22:10:50

标签: android android-fragments fragment android-fragmentactivity android-sharedpreferences

我正在尝试在片段中实现SharedPreferences。我正在尝试显示登录用户的详细信息。但是,我在帐户Fragment上显示它时遇到问题。在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());

}

3 个答案:

答案 0 :(得分:0)

你的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;
}

答案 1 :(得分:0)

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

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

我不明白为什么它不应该反过来。

答案 2 :(得分:0)

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

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