0

我想在片段中实现SharedPreferences。我正在尝试显示登录用户的详细信息。但是,我无法在帐户Fragment上显示它。在MainActivity中显示SharedPreferences时没有问题。我正在寻找一个解决方案,以便如何迎合代码,以便它能够在一个片段中工作。片段中的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()); 

} 

回答

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

嗯,这可能工作,因为它适用于我。我把它保存在与片段:

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

然后,我让他们在mainactivity有:

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

我不明白为什么它不应该工作在反向。

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