1

I just want to know how to use SharedPrefreneces to pass data from page1 to page2 and save the data in page2.

I tried this code:

In page 1:

String strSource = source1.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences(FileName,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("sourceText",strSource);
editor.commit();

Page 2:

SharedPreferences sharedPreferences = getSharedPreferences(FileName,Context.MODE_PRIVATE);
String defaultValue = "DefaultValue";
String sourceText = sharedPreferences.getString("sourceText",defaultValue);
source1.setText(sourceText);
Michael
  • 3,093
  • 7
  • 39
  • 83
Hamza Dev
  • 15
  • 5
  • Pref save data. If you uninstall your app or clear pref then it's clear. you don't want to pass one screen to other screen. you just save and use in all screen – Joker Sep 04 '19 at 13:11
  • Reformatted code and fixed spelling error. – Michael Sep 04 '19 at 14:22
  • @MR.K can you please tell what is the code? – Hamza Dev Sep 04 '19 at 14:30
  • @HamzaDev Please follow this link may be it's help you: https://stackoverflow.com/questions/23024831/android-shared-preferences-example – Joker Sep 05 '19 at 05:12

1 Answers1

0

If you want to pass data from page1 to page2 then it is possible via intent.

Example: In Kotlin

    val intent =Intent(this,Page2Activity::class.java)
    intent.putExtra("key","data")
    startActivity(intent)