String entered using EditText is deleted when leaving the Activity

2

I'm making an application where you press a button, open a AlertDialog and enter in a EditText a text, which will be entered in the same button that has been pressed.

So far so good, the problem is that when you leave the Activity and re-enter, the text that you entered in the button does not appear, it has been deleted.

I would like to know how to keep the String that has been entered.

This is my code:

    package thirdline.schooltools;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.EditText;
    import android.widget.Button;
    import android.widget.TextView;


    public class FirstTrimesterActivity extends Activity {
    Button buttonsubject1;
    EditText edittextsubject1;


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


    buttonsubject1=(Button)findViewById(R.id.buttonsubject1);

    buttonsubject1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            AlertDialog.Builder builder= new AlertDialog.Builder(FirstTrimesterActivity.this);

            builder.setTitle("Subject:");

            final EditText edittextsubject1=new EditText(FirstTrimesterActivity.this);

            builder.setView(edittextsubject1);


                    builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            buttonsubject1.setText(edittextsubject1.getText().toString());
                        }
                    });
                    builder.setNegativeButton("Cancel",null);



            AlertDialog alert = builder.create();
            alert.show();
        }





    });


   }
 }
    
asked by RedMouse 07.12.2016 в 20:43
source

2 answers

1

The text is deleted because it is not stored anywhere, Use SharedPreferences

Using getSharedPreferences ()

answered by 07.12.2016 / 20:53
source
1

It is assumed that when you leave the activity , it is destroyed and when you return, the activity arms the view completely again, that's why your text you entered is deleted.

To complete the answer of @Elanasys, in the event onCreate() of your activity you must call its SharedPreferences and look for the text you entered

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


    buttonsubject1=(Button)findViewById(R.id.buttonsubject1);

    //Si es distinto de vacio, quiere decir que ya habia una antes se le da el texto al boton
    if(this.getValuePreference(this) != ""){
        buttonsubject1.setText(getValuePreference(this));
    }
    ....
}

private String PREFS_KEY = "mispreferencias";

public void saveValuePreference(Context context, String text) {
    SharedPreferences settings = context.getSharedPreferences(PREFS_KEY, MODE_PRIVATE);
    SharedPreferences.Editor editor;
    editor = settings.edit();
    editor.putString("valorEditText", text);
    editor.commit();
}



public String getValuePreference(Context context) {
    SharedPreferences preferences = context.getSharedPreferences(PREFS_KEY, MODE_PRIVATE);
    return preferences.getString("valorEditText", "");
}
    
answered by 07.12.2016 в 20:57