Ich versuche, die SharedPreferences in einem Fragment zu implementieren. Ich versuche, die Details des angemeldeten Benutzers anzuzeigen. Ich habe jedoch Probleme, es auf dem Konto Fragment anzuzeigen. Keine Probleme beim Anzeigen von SharedPreferences in der MainActivity. Ich suche nach einer Lösung, wie man den Code bedient, damit es in einem Fragment funktioniert.SharedPreferences in einem Fragment
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());
In diesem AccountFragment Ich versuche, die Kontodaten anzuzeigen.
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());
}