26847

Shared Preferences not working for storing/receiving data

Question:

Here I have a block of code that sends information from the current activity to another one:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tutorial); prefs = getSharedPreferences("com.amrapps.paneraautomate", MODE_PRIVATE); name = (EditText) findViewById(R.id.name); lastName = (EditText) findViewById(R.id.lastName); password = (EditText) findViewById(R.id.password); final CheckBox passwordReveal = (CheckBox) findViewById (R.id.checkbox); passwordReveal.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); } else { password.setInputType(129); } } }); } public void continueButton(View v) { String stringName = name.getText().toString(); String stringLastName = lastName.getText().toString(); String stringPassword = password.getText().toString(); SharedPreferences.Editor editor = prefs.edit(); prefs.edit().putString("name", stringName).commit(); prefs.edit().putString("lastName", stringLastName).commit(); prefs.edit().putString("password", stringPassword); editor.commit(); Intent intent = new Intent(this, MainActivity.class); startActivity(intent); }

I am trying to use the data entered there in my MainActivity like so:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE); name = prefs.getString("name", ""); lastName = prefs.getString("lastName", ""); password = prefs.getString("password", ""); // Check for null values and set default if empty if (name == "") { name = "Johnny"; } if (lastName == "") { lastName = "Appleseed"; } if (password == "") { password = "Asdf123"; }

Unfortunately, every time it runs it always sets the name to "Johnny Appleseed" even if the value is not null!

Answer1:

The getSharedPreferences("com.amrapps.paneraautomate", MODE_PRIVATE) and getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE); should use the same string as the first parameter

Recommend

  • Android Facebook Integration - Predefined Wall Post
  • Android SharedPreferences String Set - some items are removed after app restart
  • Android : Alarm for update more widget
  • Error trying to use GSON with own class
  • How to read data from text file then overwrite it with new data?
  • Save String (Cookie) to SharedPrefs caused NullPointerException
  • How to create a folder inside a folder
  • What's the best way to store an id in local?
  • get SharedPreferences from a service
  • Getting list of table names from database in Android application
  • ExifInterface returns null for all Tags
  • Android - Cannot retrieve data from Firebase
  • SharedPreference problem in android
  • SharedPreference problem in android
  • write private file to internal storage
  • Shared Preferences loading multiple values
  • How to save state of CheckBox while scrolling in ListView?
  • posting multidimensional array from checkbox
  • Android Custom dialog inflating from layout - alignment issue
  • LINQ to Entities does not recognize the method 'System.String ToString()' method, and this
  • jQuery Slick Slider showing some empty slides
  • why does .next() give me 'undefined'
  • Null ImageView Reference
  • How to name a 'group' check box in Adobe Reader when wanting to fill form by FDF / XFDF
  • Android onKey w/ virtual keyboard
  • Add reference to ASP.NET 5 Class Library from Framework 4.5 Class Library Project
  • Linq Merge lists
  • Angularjs pass function from Controller to Directive (or call controller function from directive) -
  • Authentication in Play! and RestEasy
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • MongoError: Incorrect arguments
  • Django rest serializer Breaks when data exists
  • how to adjust image in a panel in Java swing?
  • Lost migrations and Azure database is now out of sync
  • Possible to stop flickering java tooltip in heavyweight mode?
  • How to set my toolbar fixed while scrolling android
  • JTable with a ScrollPane misbehaving
  • Authorize attributes not working in MVC 4
  • UserPrincipal.Current returns apppool on IIS