package com.mukeshdak.daknotes; import androidx.appcompat.app.AppCompatActivity; import android.content.SharedPreferences; import android.os.Bundle; import android.widget.EditText; public class MainActivity extends AppCompatActivity { EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.editText); // Get the value of shared preferences back. SharedPreferences getShared = getSharedPreferences("mdnote", MODE_PRIVATE); String value = getShared.getString("task1", "Type a note and save"); editText.setText(value); } @Override public void onBackPressed() { editText = findViewById(R.id.editText); String msg = editText.getText().toString(); SharedPreferences shrd = getSharedPreferences("mdnote", MODE_PRIVATE); SharedPreferences.Editor editor = shrd.edit(); editor.putString("task1", msg); editor.apply(); // Toast.makeText(MainActivity.this,"Text Saved", Toast.LENGTH_LONG).show(); super.onBackPressed(); } }