조각에 SharedPreferences를 구현하려고합니다. 로그인 한 사용자의 세부 정보를 표시하려고합니다. 그러나 계정 단편에 표시하는 데 문제가 있습니다. MainActivity에 SharedPreferences를 표시 할 때 아무런 문제가 없습니다. 나는 코드를 어떻게 처리 할 것인가에 대한 해결책을 찾고있다. 그래서 조각에서 작동 할 것이다. 이 AccountFragment에서조각 내의 SharedPreferences
if(!SharedPrefManager.getInstance(this).isLoggedIn()){
finish();
startActivity(new Intent(this, LoginActivity.class));
}
textviewUsername = (TextView)findViewById(R.id.usernameLabel);
textviewUsername.setText(SharedPrefManager.getInstance(this).getUsername());
MainActivity.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());
}