title: Reading SharedPreferences data in Fragment, within FragmentActivity
body: <p>I'm trying to save the name the user enters on the log in screen below <code>Settings.java</code> to a <code>sharedPreference</code>, which will be read by <code>TestFragment2.java</code>, which is called from another FragmentActivity, <code>SampleTabsDefault.java</code></p>
  
  <p><img src="https://i.stack.imgur.com/TbXEj.jpg" alt="Log in Screen">
  <b>Settings.java</b></p>
  
  <pre><code>    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();
      }
  
  }
  </code></pre>
  
  <p><code>Intent myIntent = new Intent("com.test.SAMPLETABSDEFAULT");startActivity(myIntent);</code>
  loads the <code>SampleTabsDefault.java</code> just fine, and so do all the fragments it calls. </p>
  
  <pre><code> 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;
  
  
      }
  </code></pre>
  
  <p>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 <code>sharedPreferences</code> inside <code>TestFragment2.java</code> 
  <img src="https://i.stack.imgur.com/Z0EF7.jpg" alt="SampleTabsDefault.java and TestFragment2.java">
  <b>TestFragment2.java</b></p>
  
  <pre><code> @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);
  }
  </code></pre>
  
  <p>In <code>TestFragment2.java</code> Im trying to read and apply to <code>textView</code> via 'setText();' the sharedPreferences the same way I do on <code>Settings.java</code>. The code in <code>Settings.java</code> does work, <code>String name = sp.getString("NAME", "YourName");</code>it loads the <code>sharedPreference</code> and displays the name entered by the user last time application was ran , but in  <code>TestFragmnet2.java</code> the same block of code returns YourName from <code>String name = sp.getString("NAME", "YourName");</code></p>
  
  <p>Any help is greatly appreciated, thanks!!</p>