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