0
    sharedPreferences = getSharedPreferences("HTTP_HELPER_PREFS", Context.MODE_PRIVATE);
    editor = sharedPreferences.edit();

    // get the IP address and port number from the last time the user used the app,
    // put an empty string "" is this is the first time.
    editTextIPAddress.setText(sharedPreferences.getString(PREF_IP, ""));
    editTextPortNumber.setText(sharedPreferences.getString(PREF_PORT, ""));
    confbutton.setOnClickListener(this);

}


@Override
public void onClick(View v) {
    // get the ip address
    String ipAddress = editTextIPAddress.getText().toString().trim();
    // get the port number
    String portNumber = editTextPortNumber.getText().toString().trim();

    editor.putString(PREF_IP, ipAddress); // 
    editor.putString(PREF_PORT, portNumber); 
    editor.commit(); 
    onBackPressed();

that's my code i created a second Activity to get the ip adresse and the port number now i need to read that SharedPreferencesin the main activity

Ela Hidri
  • 49
  • 8

1 Answers1

0

Something like this

SharedPreferences prefs = getSharedPreferences("HTTP_HELPER_PREFS", Context.MODE_PRIVATE);
String ipAddress = prefs.getString(PREF_IP, ipAddress); 
String portNumber = prefs.getString(PREF_PORT, portNumber);
Daniil
  • 1,290
  • 11
  • 17
  • if i write this in the main activity i will get the ip address and port number values? – Ela Hidri Apr 30 '16 at 19:29
  • Yes, SharedPreferences are called shared and that means you can access them from any part of your app. Even after shutting down the app data will stay there. – Daniil Apr 30 '16 at 19:36
  • Read the link above to get more info about `SharedPreferences`. But i don't think it's the right way to do that. It's better when activities interact by `Intent`s and `startActivityForResult` method. – Daniil Apr 30 '16 at 19:40
  • I tried what you told me to do it works but it only gives me the last value if I tried to change it it won't change only if I kill the app and reopen it can u explain the intents and start activity for rrsult .. Thank you very mutch :)) – Ela Hidri Apr 30 '16 at 19:51