Search code examples
androidandroid-fragmentsfragmentandroid-fragmentactivityandroid-sharedpreferences

SharedPreferences in a Fragment


I am trying to implement the SharedPreferences in a fragment. I am trying to display the details of the logged in user. However, I am having trouble displaying it on the account Fragment. No problems when displaying SharedPreferences in the MainActivity. I am looking for a solution as to how to cater the code so it will work in a 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 this AccountFragment I am trying to display the account details.

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

}

Solution

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