title: Android - How to get data in a shared preference of one activity to another activity?
body: <p>I have the following two activity I want to send the values present in'name' and 'subdivision' present in the shared preference to the second activity. I am able to get the values using shared preference but I'm not able to send that value to the second activity. Below is my code...Can anyone help me resolve this ?</p>
  
  <pre><code>public class UidScreen extends Activity {
      EditText editrexts, edits, edites, editess;
      Button buttons;
      String userId;
      LayoutTwoActivity layoutTwoActivity;
      DatapushService dsp = null;
      CheckBox chkSignedIn;
      SharedPreferences settings;
      private static final String PREFRENCES_NAME = "myprefrences";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.uid_screen);
          editrexts = (EditText) findViewById(R.id.editText1);
          edits = (EditText) findViewById(R.id.editText2);
          edites = (EditText) findViewById(R.id.editText3);
          editess = (EditText) findViewById(R.id.editText4);
          chkSignedIn = (CheckBox) findViewById(R.id.chksingin);
          //userId = editrexts.getText().toString();
          settings = getSharedPreferences(PREFRENCES_NAME,
                  Context.MODE_PRIVATE);
          String name = settings.getString("name", "");
          String division = settings.getString("Division", "");
          String Subdivision = settings.getString("Subdivison", "");
          String range = settings.getString("Range", "");
  
  
          editrexts.setText(name);
          edits.setText(division);
          edites.setText(Subdivision);
          editess.setText(range);
  
  
          buttons = (Button) findViewById(R.id.button1);
  
          buttons.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  //LayoutTwoActivity ly=new LayoutTwoActivity();
                  //dsp = new DatapushService();
                  userId = editrexts.getText().toString();
  
                  AssetDetails assetDetails = new AssetDetails();
                  assetDetails.setUserID(userId);
                  //ly.assetDetails =assetDetails;
                  //dsp.assetDetails =assetDetails;
                  Databaseuid ui = new Databaseuid(UidScreen.this);
                  ui.open();
                  String Division = ui.getDivision(userId);
                  String Subdivision = ui.getSubdivision(userId);
                  String Range = ui.getRange(userId);
                  edits.setText(Division);
                  edites.setText(Subdivision);
                  editess.setText(Range);
                  if (chkSignedIn.isChecked()) {
  
                      showToast("User Name and Password Saved!!!");
  
                      SharedPreferences settings = getSharedPreferences(
                              PREFRENCES_NAME, Context.MODE_PRIVATE);
                      settings.edit().putString("name", userId).putString("Division",Division).putString("Subdivison", Subdivision).putString("Range", Range).commit();
  
                      showToast("Restart App And see it works!!!");
                  } else {
                      showToast("Tick keep me logged in!!!");
  
                  }
  
  
                  ui.close();
              }
          });
  
  
      }
  
      public void opennewactivity(View view) {
          // Do something in response to button !! Used to call MainActivity Class on click of the image !
          Intent intent = new Intent(this, MainActivity.class);
          intent.putExtra("str4", userId);
  
  
          startActivity(intent);
      }
  
      private void showToast(String msg) {
          Toast.makeText(UidScreen.this, msg, Toast.LENGTH_LONG).show();
  
  
      }
  }
  </code></pre>
  
  <p>2nd Activity:</p>
  
  <pre><code>public class LayoutTwoActivity extends Activity {
  
  String pref,pref2;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          setContentView(R.layout.xml_second);
          //SharedPreference settings;
          SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
          pref = settings.getString("name", "");
          pref2 = settings.getString("Subdivison",null);
          Toast.makeText(getApplicationContext(), pref,
                  Toast.LENGTH_LONG).show();
  
  }
          }
  </code></pre>