amirofff
amirofff

Reputation: 5

passing EditText content by SharedPreference

In my application i have 2 activities , I want to type a URL address in EditText of SettingsActivity and use it in MainActivity but i can't ...

This is my SettingsActivity :

 public class SettingsActivity extends Activity {

Switch aSwitch;
EditText editText;
String URL_ENDPOINT;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    editText = (EditText) findViewById(R.id.etEnd);
    SharedPreferences sp = getSharedPreferences("edittext", Activity.MODE_PRIVATE);
    SharedPreferences.Editor edt = sp.edit();
    String URLValue = editText.getText().toString();
    edt.putString("URL", URLValue);
    edt.commit();
    editText.setText(sp.getString("URL" , URLValue));
}

And this is my MainActivity :

 public class MainActivity extends Activity {


String URL_Endpoint;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_monitoring);


    //get URL for endpoint from SettingsActivity
    SharedPreferences sp = getSharedPreferences("edittext", Activity.MODE_PRIVATE);
    String URL = sp.getString("URL", "");
    URL_Endpoint = URL;

Upvotes: 0

Views: 44

Answers (2)

Pier Giorgio Misley
Pier Giorgio Misley

Reputation: 5351

First, have a look at Android's lifecycle

enter image description here

Now, that said, onCreate method is just called once when the activity is created, as you can see in the picture.

For this reason, getting the text of the editText in the onCreate method will result in an empty text (if not pre-populated).

Since I think you want to create a settings activity which set up some stuffs for the mainActivity, you have two ways for achieving your goal:

  1. You can add a "Save" button. I will reccomend this since the user might input something and change his mind, and this way clicking "back" will cancel the operation.
  2. You can save the editText value at onPause of the SettingsActivity. This method will override the value every time the user exits the activity no matter what.

Note: remember to popolate the text of settingsActivity in the onCreate/onResume, or it always will be empty.

Also another point is that, always as you can see in the picture, once you come back from settings activity onCreate is not called again. So you have, in onResume method, to take the value from the SharedPreference and put it in your MainActivity's textview.

First, i reccomend you to study that lifeCycle because it's really a core information for programming Android.

Upvotes: 1

lcukerd
lcukerd

Reputation: 31

SharedPreferences sp = getSharedPreferences("edittext", Activity.MODE_PRIVATE);
SharedPreferences.Editor edt = sp.edit();
String URLValue = editText.getText().toString();
edt.putString("URL", URLValue);
edt.commit();
editText.setText(sp.getString("URL" , URLValue));

Put this code in your onPause() method

Upvotes: 0

Related Questions