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();
}
});
}
}