0

The EditButton of my app in Android Studio can be edited but once you have edited the texts it will not save when you exit the window. What to do?

 public class BellPepperActivity extends AppCompatActivity {

    TextView bpTextView;
    AlertDialog dialog;
    EditText editText;

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

        bpTextView = (TextView) findViewById(R.id.bpTextView);
        dialog = new AlertDialog.Builder(this).create();
        editText = new  EditText(this);
        dialog.setTitle("BELL PEPPER");

        dialog.setView(editText);

        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "SAVE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                bpTextView.setText(editText.getText());
            }
        });

        bpTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setText(bpTextView.getText());
                dialog.show();
            }
        });


    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Marceline
  • 1
  • 2
  • What actually you want to do? – Faysal Ahmed Feb 27 '18 at 17:57
  • when i edit the text it would send when you exit the window or app, in my case the text can be edited but once you exited the windows for editing the text you edited will go back to the original text – Marceline Feb 27 '18 at 18:01
  • Make edit text final and then access value from edit text. This will help you: https://stackoverflow.com/questions/18799216/how-to-make-a-edittext-box-in-a-dialog – Faysal Ahmed Feb 27 '18 at 18:04

1 Answers1

0

By window, I assume you Activity (the AppCompatActivity that you have created). To maintain state in Activities you have to learn about the activity lifecycle. Basically when you leave you have to save the instance state:

// invoked when the activity may be temporarily destroyed, save the instance state here
@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putString(TEXT_VIEW_KEY, editText.getText());

    // call superclass to save any view hierarchy
    super.onSaveInstanceState(outState);
}

and when you restore the state you do the same:

// This callback is called only when there is a saved instance previously saved using // onSaveInstanceState(). We restore some state in onCreate() while we can optionally restore

// other state here, possibly usable after onStart() has completed.
// The savedInstanceState Bundle is same as the one used in onCreate().
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    editText.setText(savedInstanceState.getString(TEXT_VIEW_KEY));
}

Obviously you have to create a TEXT_VIEW_KEY as private final string at the top of your class:

private static final String TEXT_VIEW_KEY = "TEXT_VIEW_KEY";

Untested, but that should work for you now. For more advanced lifecycle handling learn about the Android Architecture Components, but that should wait until you understand the basic activity lifecycle in android App.

Kris Erickson
  • 33,454
  • 26
  • 120
  • 175