I'm trying to save the name the user enters on the log in screen below Settings.java to a sharedPreference, which will be read by TestFragment2.java, which is called from another FragmentActivity, SampleTabsDefault.java
Settings.java
private void savePrefs(String key, boolean value) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putBoolean(key, value);
edit.commit();
}
private void savePrefs(String key, String value) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putString(key, value);
edit.commit();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savePrefs("CHECKBOX", cb.isChecked());
if (cb.isChecked())
savePrefs("NAME", et.getText().toString());
Intent myIntent = new Intent("com.test.SAMPLETABSDEFAULT");
startActivity(myIntent);
finish();
}
}
Intent myIntent = new Intent("com.test.SAMPLETABSDEFAULT");startActivity(myIntent);
loads the SampleTabsDefault.java just fine, and so do all the fragments it calls.
class NavTabs extends FragmentPagerAdapter {
public NavTabs(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position)
{
case 0:
TestFragment fragment = new TestFragment();
return fragment;
case 1:
TestFragment2 fragment2 = new TestFragment2();
return fragment2;
}
TestFragment3 fragment3 = new TestFragment3();
return fragment3;
}
I can scroll left right and get to fragment TestFragment2.java seen below....however its not reading, or Im making a very bonehead move applying the sharedPreferences inside TestFragment2.java
TestFragment2.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_2, container, false);
text = (TextView)myView.findViewById(R.id.textView1);
et = (TextView)myView.findViewById(R.id.textView3);
cb = (CheckBox) myView.findViewById(R.id.checkBox1);
loadPrefs();
return myView;
}
private void loadPrefs() {
SharedPreferences sp = getActivity().getSharedPreferences("CHECKBOX", 0);
boolean cbValue = sp.getBoolean("CHECKBOX", false);
String name = sp.getString("NAME", "YourName");
if(cbValue){
cb.setChecked(true);
}else{
cb.setChecked(false);
}
et.setText(name);
}
In TestFragment2.java Im trying to read and apply to textView via 'setText();' the sharedPreferences the same way I do on Settings.java. The code in Settings.java does work, String name = sp.getString("NAME", "YourName");it loads the sharedPreference and displays the name entered by the user last time application was ran , but in TestFragmnet2.java the same block of code returns YourName from String name = sp.getString("NAME", "YourName");
Any help is greatly appreciated, thanks!!
I think that you are calling
SharedPreferences sp = getActivity().getSharedPreferences("CHECKBOX", 0);
incorrectly in TestFragment2's loadPrefs() ,method
From the docs the first param (name) is:
Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).
So it looks like you are creating a new prefernces file called "CHECKBOX" in TestFragment2.
maybe change loadPrefs() to the same usage found in Settings.java so you can call up the same preferences used earlier like so:
private void loadPrefs() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity())
boolean cbValue = sp.getBoolean("CHECKBOX", false);
String name = sp.getString("NAME", "YourName");
if(cbValue){
cb.setChecked(true);
}else{
cb.setChecked(false);
}
et.setText(name);
}
来源:https://stackoverflow.com/questions/15031825/reading-sharedpreferences-data-in-fragment-within-fragmentactivity