Search code examples
androidandroid-layoutandroid-activitytextview

How can I save my TextView Android Studio


I tried the edit my TextView by clicking. I did manage the edit but it cannot saved. When I close and re-open app or change activity its returning the default.

 textView = (TextView) findViewById(R.id.textView5);
    dialog = new AlertDialog.Builder(this).create();
    editText = new EditText(this);

    dialog.setTitle("Edit the text.");
    dialog.setView(editText);

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

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

What am I missing?


Solution

  • TextView exists only on-screen. Once the Activity is closed, the data is gone.

    If you want to store this persistently you will need to store the data in SharedPreferences, or in an SQLite database or in a file.