Advertisement
mukeshdak

2021-08-28_Android_Shared_prefrence

Aug 28th, 2021
1,209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. package com.mukeshdak.daknotes;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.SharedPreferences;
  4. import android.os.Bundle;
  5. import android.widget.EditText;
  6.  
  7. public class MainActivity extends AppCompatActivity {
  8.  
  9.     EditText editText;
  10.  
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.  
  16.         editText = findViewById(R.id.editText);
  17.  
  18.         // Get the value of shared preferences back.
  19.         SharedPreferences getShared = getSharedPreferences("mdnote", MODE_PRIVATE);
  20.         String value = getShared.getString("task1", "Type a note and save");
  21.         editText.setText(value);
  22.     }
  23.  
  24.     @Override
  25.     public void onBackPressed() {
  26.  
  27.         editText = findViewById(R.id.editText);
  28.         String msg = editText.getText().toString();
  29.  
  30.         SharedPreferences shrd = getSharedPreferences("mdnote", MODE_PRIVATE);
  31.         SharedPreferences.Editor editor = shrd.edit();
  32.  
  33.         editor.putString("task1", msg);
  34.         editor.apply();
  35.  
  36.     //    Toast.makeText(MainActivity.this,"Text Saved", Toast.LENGTH_LONG).show();
  37.  
  38.         super.onBackPressed();
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement