1

So, i was trying to save some small data such as username of the user (remember me feature) and sharedpreference doesnt seems to save any data into my android device. i wonder why, and there's nothing wrong with my code...

final Button btnLogin = (Button) findViewById(R.id.btn_Login);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                tvName = (EditText) findViewById(R.id.edit_mName);

                tvPassword = (EditText) findViewById(R.id.edit_mPassword);

                try {
                    if (cbRme.isChecked()) {
                        SharedPreferences prefs = getSharedPreferences(
                                PREFS_NAME, Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = prefs.edit();
                        editor.clear();
                        editor.putString("keyusername", tvName.getText()
                                .toString());
                        editor.putString("keypassword", tvPassword
                                .getText().toString());
                        editor.putBoolean("keycheckbox", true);
                        editor.commit();


                    } else {
                        SharedPreferences prefs = getSharedPreferences(
                                PREFS_NAME, Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = prefs.edit();
                        editor.clear();
                        editor.remove("keyusername");
                        editor.remove("keypassword");
                        editor.remove("keycheckbox");
                        editor.commit();
                    }
                } catch (Exception e) {
                    Toast.makeText(getBaseContext(), e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }

    public void onResume() {

    SharedPreferences prefs = getSharedPreferences("project", 0);
    username = prefs.getString("keyusername", DEFAULT);
    password = prefs.getString("keypassword", DEFAULT);
    checkbox = prefs.getBoolean("keycheckbox", false);

    if ((username.equals(DEFAULT)) || checkbox == false) {
        Toast.makeText(getBaseContext(), "No data found", Toast.LENGTH_LONG)
                .show();
    } else {
        Toast.makeText(getBaseContext(), "Data is found.",
                Toast.LENGTH_LONG).show();

        setUsername();
        setCheckedBox();
    }

    super.onResume();
}

private void setUsername() {
    EditText tvName = (EditText) findViewById(R.id.edit_mName);
    tvName.setText(username);
}

private void setCheckedBox() {
    cbRme = (CheckBox) findViewById(R.id.cbRememberMe);
    cbRme.setChecked(checkbox);
}
Minorsee
  • 67
  • 10

1 Answers1

1

You are trying to set data in below shared pref :

   SharedPreferences prefs = getSharedPreferences(
                                PREFS_NAME, Context.MODE_PRIVATE);

And trying to get data from below shared pref :

SharedPreferences prefs = getSharedPreferences("project", 0);

which seems to be 2 different shared pref

EDIT :

Shared pref are not visible in device because of security reasons but if device is rooted you could view the shared pref files.

To debug and view the shared pref file , you can used emulator where shared pref are visible and you can pull and push the shared pref file .

KOTIOS
  • 11,177
  • 3
  • 39
  • 66
  • noted for that, that was a mistake from me. even so, i should be getting a .xml file if data is being saved, but not for my case. i didn't get any .xml file saved. – Minorsee Feb 15 '15 at 16:35
  • you mean shared pref files isnt created? – KOTIOS Feb 15 '15 at 16:36
  • yeap, shared pref files – Minorsee Feb 15 '15 at 16:38
  • ok, how are you checking that? are you on device or emulator? – KOTIOS Feb 15 '15 at 16:39
  • im on device, i check it through eclipse, they create 4 folders for me, but none of them contain shared pref files – Minorsee Feb 15 '15 at 16:40
  • can u name folder? are you chking in files under ur package name? – KOTIOS Feb 15 '15 at 16:41
  • yeap, i check the folder under the package name. and i have try catch as well, to make give me notice on whether the data is being saved or nah – Minorsee Feb 15 '15 at 17:12
  • yes but can u name the files plz? i wanna know names – KOTIOS Feb 15 '15 at 17:16
  • samsung-gt_xxxx > com.example.xxx > data > . and also, when i try to save it right, it catches error but with a blank error message. – Minorsee Feb 15 '15 at 17:20
  • hey data comes first the ur package name right? isnt it? – KOTIOS Feb 15 '15 at 17:23
  • take a screen shot of ur file explorer sturutre and share a link here – KOTIOS Feb 15 '15 at 17:24
  • http://i.imgur.com/RvdB5Ck.jpg?1 here it is, when i open "data" folder it give nothing – Minorsee Feb 15 '15 at 17:30
  • ok , actually on android device it does not hv permission to view data/data/com.packagename/ files for security reasons hence you are not able to view it but it does not mean that shared pref files isnt created, one suggestion from mu side is try to run ur code on emulator only then you would see that in data/data/urpackagename/sharedpref/ ur file is created. hope u got the idea, let me know in case of nay issues – KOTIOS Feb 15 '15 at 17:35
  • ah, i see. yes, i have tried using emulator and it does work. hmm, is there anyway that i could actually allow that folder to be view? – Minorsee Feb 15 '15 at 17:39
  • http://stackoverflow.com/questions/13006315/how-to-access-data-data-folder-in-android-device i found it here – Minorsee Feb 15 '15 at 17:40
  • thanks alot diva, for guiding me. really appreciate it. many thanksss – Minorsee Feb 15 '15 at 18:46
  • basically to view that folder you need to root the device , hope my answer helped u understand , you may accept it if you found this soulction helpful – KOTIOS Feb 16 '15 at 03:38
  • yes, i get what u mean, and have solved the problem with your guidance, thanks alot, appreciate it! – Minorsee Feb 16 '15 at 18:02