Retrieve data using SharedPreferences












0















I'm trying to get data (Name , Email , Password) from user and then save them using SharedPreferences . In second Activity I want to retrieve the data (only Email and Password) and then set them in editText. Here is my code. My data is not showing and I don't know where the problem is.



My code :-



First Activity



EditText username  , userpassword , useremail ;

SharedPreferences sharedPreferences ;


static final String Email = "Email";
static final String Username = "Username";
static final String Password = "Password";

public void RegisterrButton(View view){

username = findViewById(R.id.username_editText);
userpassword = findViewById(R.id.pasword_editText);
useremail = findViewById(R.id.email_editText);

String name = username.getText().toString();
String password = userpassword.getText().toString();
String email = useremail.getText().toString();

SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putString(Username , name);
editor.putString(Password , password);
editor.putString(Email,email);
editor.commit();


Intent intent = new Intent(signup.this,MainActivity.class);
startActivity(intent);


}


Second Activity



SharedPreferences sharedPreferences ;



static final String prefrence = "prefrence";

static final String Email = "Email";
static final String Password = "Password";

EditText useremail , userpassword ;

public void EnterButton(View view){

useremail = findViewById(R.id.useremail_editText);
userpassword = findViewById(R.id.pasword_editText);


sharedPreferences = getSharedPreferences(prefrence , Context.MODE_PRIVATE) ;

if(sharedPreferences.contains(Email) ){

useremail.setText(sharedPreferences.getString(Email , ""));}


if(sharedPreferences.contains(Password)){

userpassword.setText(sharedPreferences.getString(Password , ""));

}

}









share|improve this question

























  • If your activity is not huge, can you please copy and paste the code of the entire class? or maybe at least the parts which contribute to this code i.e. Username (what is that? a variable? object?)

    – Nero
    Nov 20 '18 at 18:17











  • are you saving email and password in shared pref? not a good idea.

    – soldfor
    Nov 20 '18 at 18:19











  • @Nero I update the code

    – Noor
    Nov 20 '18 at 18:20











  • Use putExtra() from an Intent object instead.

    – Barns
    Nov 20 '18 at 18:26
















0















I'm trying to get data (Name , Email , Password) from user and then save them using SharedPreferences . In second Activity I want to retrieve the data (only Email and Password) and then set them in editText. Here is my code. My data is not showing and I don't know where the problem is.



My code :-



First Activity



EditText username  , userpassword , useremail ;

SharedPreferences sharedPreferences ;


static final String Email = "Email";
static final String Username = "Username";
static final String Password = "Password";

public void RegisterrButton(View view){

username = findViewById(R.id.username_editText);
userpassword = findViewById(R.id.pasword_editText);
useremail = findViewById(R.id.email_editText);

String name = username.getText().toString();
String password = userpassword.getText().toString();
String email = useremail.getText().toString();

SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putString(Username , name);
editor.putString(Password , password);
editor.putString(Email,email);
editor.commit();


Intent intent = new Intent(signup.this,MainActivity.class);
startActivity(intent);


}


Second Activity



SharedPreferences sharedPreferences ;



static final String prefrence = "prefrence";

static final String Email = "Email";
static final String Password = "Password";

EditText useremail , userpassword ;

public void EnterButton(View view){

useremail = findViewById(R.id.useremail_editText);
userpassword = findViewById(R.id.pasword_editText);


sharedPreferences = getSharedPreferences(prefrence , Context.MODE_PRIVATE) ;

if(sharedPreferences.contains(Email) ){

useremail.setText(sharedPreferences.getString(Email , ""));}


if(sharedPreferences.contains(Password)){

userpassword.setText(sharedPreferences.getString(Password , ""));

}

}









share|improve this question

























  • If your activity is not huge, can you please copy and paste the code of the entire class? or maybe at least the parts which contribute to this code i.e. Username (what is that? a variable? object?)

    – Nero
    Nov 20 '18 at 18:17











  • are you saving email and password in shared pref? not a good idea.

    – soldfor
    Nov 20 '18 at 18:19











  • @Nero I update the code

    – Noor
    Nov 20 '18 at 18:20











  • Use putExtra() from an Intent object instead.

    – Barns
    Nov 20 '18 at 18:26














0












0








0


0






I'm trying to get data (Name , Email , Password) from user and then save them using SharedPreferences . In second Activity I want to retrieve the data (only Email and Password) and then set them in editText. Here is my code. My data is not showing and I don't know where the problem is.



My code :-



First Activity



EditText username  , userpassword , useremail ;

SharedPreferences sharedPreferences ;


static final String Email = "Email";
static final String Username = "Username";
static final String Password = "Password";

public void RegisterrButton(View view){

username = findViewById(R.id.username_editText);
userpassword = findViewById(R.id.pasword_editText);
useremail = findViewById(R.id.email_editText);

String name = username.getText().toString();
String password = userpassword.getText().toString();
String email = useremail.getText().toString();

SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putString(Username , name);
editor.putString(Password , password);
editor.putString(Email,email);
editor.commit();


Intent intent = new Intent(signup.this,MainActivity.class);
startActivity(intent);


}


Second Activity



SharedPreferences sharedPreferences ;



static final String prefrence = "prefrence";

static final String Email = "Email";
static final String Password = "Password";

EditText useremail , userpassword ;

public void EnterButton(View view){

useremail = findViewById(R.id.useremail_editText);
userpassword = findViewById(R.id.pasword_editText);


sharedPreferences = getSharedPreferences(prefrence , Context.MODE_PRIVATE) ;

if(sharedPreferences.contains(Email) ){

useremail.setText(sharedPreferences.getString(Email , ""));}


if(sharedPreferences.contains(Password)){

userpassword.setText(sharedPreferences.getString(Password , ""));

}

}









share|improve this question
















I'm trying to get data (Name , Email , Password) from user and then save them using SharedPreferences . In second Activity I want to retrieve the data (only Email and Password) and then set them in editText. Here is my code. My data is not showing and I don't know where the problem is.



My code :-



First Activity



EditText username  , userpassword , useremail ;

SharedPreferences sharedPreferences ;


static final String Email = "Email";
static final String Username = "Username";
static final String Password = "Password";

public void RegisterrButton(View view){

username = findViewById(R.id.username_editText);
userpassword = findViewById(R.id.pasword_editText);
useremail = findViewById(R.id.email_editText);

String name = username.getText().toString();
String password = userpassword.getText().toString();
String email = useremail.getText().toString();

SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putString(Username , name);
editor.putString(Password , password);
editor.putString(Email,email);
editor.commit();


Intent intent = new Intent(signup.this,MainActivity.class);
startActivity(intent);


}


Second Activity



SharedPreferences sharedPreferences ;



static final String prefrence = "prefrence";

static final String Email = "Email";
static final String Password = "Password";

EditText useremail , userpassword ;

public void EnterButton(View view){

useremail = findViewById(R.id.useremail_editText);
userpassword = findViewById(R.id.pasword_editText);


sharedPreferences = getSharedPreferences(prefrence , Context.MODE_PRIVATE) ;

if(sharedPreferences.contains(Email) ){

useremail.setText(sharedPreferences.getString(Email , ""));}


if(sharedPreferences.contains(Password)){

userpassword.setText(sharedPreferences.getString(Password , ""));

}

}






android sharedpreferences






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 18:19







Noor

















asked Nov 20 '18 at 18:10









NoorNoor

267




267













  • If your activity is not huge, can you please copy and paste the code of the entire class? or maybe at least the parts which contribute to this code i.e. Username (what is that? a variable? object?)

    – Nero
    Nov 20 '18 at 18:17











  • are you saving email and password in shared pref? not a good idea.

    – soldfor
    Nov 20 '18 at 18:19











  • @Nero I update the code

    – Noor
    Nov 20 '18 at 18:20











  • Use putExtra() from an Intent object instead.

    – Barns
    Nov 20 '18 at 18:26



















  • If your activity is not huge, can you please copy and paste the code of the entire class? or maybe at least the parts which contribute to this code i.e. Username (what is that? a variable? object?)

    – Nero
    Nov 20 '18 at 18:17











  • are you saving email and password in shared pref? not a good idea.

    – soldfor
    Nov 20 '18 at 18:19











  • @Nero I update the code

    – Noor
    Nov 20 '18 at 18:20











  • Use putExtra() from an Intent object instead.

    – Barns
    Nov 20 '18 at 18:26

















If your activity is not huge, can you please copy and paste the code of the entire class? or maybe at least the parts which contribute to this code i.e. Username (what is that? a variable? object?)

– Nero
Nov 20 '18 at 18:17





If your activity is not huge, can you please copy and paste the code of the entire class? or maybe at least the parts which contribute to this code i.e. Username (what is that? a variable? object?)

– Nero
Nov 20 '18 at 18:17













are you saving email and password in shared pref? not a good idea.

– soldfor
Nov 20 '18 at 18:19





are you saving email and password in shared pref? not a good idea.

– soldfor
Nov 20 '18 at 18:19













@Nero I update the code

– Noor
Nov 20 '18 at 18:20





@Nero I update the code

– Noor
Nov 20 '18 at 18:20













Use putExtra() from an Intent object instead.

– Barns
Nov 20 '18 at 18:26





Use putExtra() from an Intent object instead.

– Barns
Nov 20 '18 at 18:26












4 Answers
4






active

oldest

votes


















0














Replace the following code in MainActivity:



SharedPreferences.Editor editor = sharedPreferences.edit();


With



public static final String MY_PREFS_NAME = "prefrence";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();


Use the same MY_PREFS_NAME variable to get the info in your second activity:



SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 





share|improve this answer
























  • Thanks it worked :)

    – Noor
    Nov 20 '18 at 18:42











  • Your welcome :)

    – Nero
    Nov 20 '18 at 18:45



















0














Make sure are you are using same shared preference name at the time of persisting and reading the values.



    SharedPreferences preference = context.getSharedPreferences(<SAME_PREFERENCE_NAME>,Context.MODE_PRIVATE);





share|improve this answer































    0














    Create your Function and try this:



       public void saveText(String key, String value) {
    SharedPreferences sPref = context.getSharedPreferences(config.PREFERENCES, context.MODE_PRIVATE);
    SharedPreferences.Editor ed = sPref.edit();
    ed.putString(key, value);
    ed.commit();
    ed.apply();
    toast("Yatda saklandy");
    }

    public String loadText(String key) {
    SharedPreferences sPref = context.getSharedPreferences(config.PREFERENCES, context.MODE_PRIVATE);
    String savedText = sPref.getString(key, "");
    return savedText;
    }


    and use it:



    saveText("name",username.getText().toString());

    loadText("name");





    share|improve this answer































      0














      In your first activity you are not initialising sharedPreference
      To initialise do this



      sharedPreferences = getSharedPreferences(prefrence , Context.MODE_PRIVATE);


      Consider using apply().



      It writes the changes to the RAM immediately and waits and writes it to the internal storage(the actual preference file) after. Commit writes the changes synchronously and directly to the file.






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399028%2fretrieve-data-using-sharedpreferences%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Replace the following code in MainActivity:



        SharedPreferences.Editor editor = sharedPreferences.edit();


        With



        public static final String MY_PREFS_NAME = "prefrence";
        SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();


        Use the same MY_PREFS_NAME variable to get the info in your second activity:



        SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 





        share|improve this answer
























        • Thanks it worked :)

          – Noor
          Nov 20 '18 at 18:42











        • Your welcome :)

          – Nero
          Nov 20 '18 at 18:45
















        0














        Replace the following code in MainActivity:



        SharedPreferences.Editor editor = sharedPreferences.edit();


        With



        public static final String MY_PREFS_NAME = "prefrence";
        SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();


        Use the same MY_PREFS_NAME variable to get the info in your second activity:



        SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 





        share|improve this answer
























        • Thanks it worked :)

          – Noor
          Nov 20 '18 at 18:42











        • Your welcome :)

          – Nero
          Nov 20 '18 at 18:45














        0












        0








        0







        Replace the following code in MainActivity:



        SharedPreferences.Editor editor = sharedPreferences.edit();


        With



        public static final String MY_PREFS_NAME = "prefrence";
        SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();


        Use the same MY_PREFS_NAME variable to get the info in your second activity:



        SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 





        share|improve this answer













        Replace the following code in MainActivity:



        SharedPreferences.Editor editor = sharedPreferences.edit();


        With



        public static final String MY_PREFS_NAME = "prefrence";
        SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();


        Use the same MY_PREFS_NAME variable to get the info in your second activity:



        SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 18:24









        NeroNero

        6991419




        6991419













        • Thanks it worked :)

          – Noor
          Nov 20 '18 at 18:42











        • Your welcome :)

          – Nero
          Nov 20 '18 at 18:45



















        • Thanks it worked :)

          – Noor
          Nov 20 '18 at 18:42











        • Your welcome :)

          – Nero
          Nov 20 '18 at 18:45

















        Thanks it worked :)

        – Noor
        Nov 20 '18 at 18:42





        Thanks it worked :)

        – Noor
        Nov 20 '18 at 18:42













        Your welcome :)

        – Nero
        Nov 20 '18 at 18:45





        Your welcome :)

        – Nero
        Nov 20 '18 at 18:45













        0














        Make sure are you are using same shared preference name at the time of persisting and reading the values.



            SharedPreferences preference = context.getSharedPreferences(<SAME_PREFERENCE_NAME>,Context.MODE_PRIVATE);





        share|improve this answer




























          0














          Make sure are you are using same shared preference name at the time of persisting and reading the values.



              SharedPreferences preference = context.getSharedPreferences(<SAME_PREFERENCE_NAME>,Context.MODE_PRIVATE);





          share|improve this answer


























            0












            0








            0







            Make sure are you are using same shared preference name at the time of persisting and reading the values.



                SharedPreferences preference = context.getSharedPreferences(<SAME_PREFERENCE_NAME>,Context.MODE_PRIVATE);





            share|improve this answer













            Make sure are you are using same shared preference name at the time of persisting and reading the values.



                SharedPreferences preference = context.getSharedPreferences(<SAME_PREFERENCE_NAME>,Context.MODE_PRIVATE);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 18:21









            Ramesh YankatiRamesh Yankati

            68658




            68658























                0














                Create your Function and try this:



                   public void saveText(String key, String value) {
                SharedPreferences sPref = context.getSharedPreferences(config.PREFERENCES, context.MODE_PRIVATE);
                SharedPreferences.Editor ed = sPref.edit();
                ed.putString(key, value);
                ed.commit();
                ed.apply();
                toast("Yatda saklandy");
                }

                public String loadText(String key) {
                SharedPreferences sPref = context.getSharedPreferences(config.PREFERENCES, context.MODE_PRIVATE);
                String savedText = sPref.getString(key, "");
                return savedText;
                }


                and use it:



                saveText("name",username.getText().toString());

                loadText("name");





                share|improve this answer




























                  0














                  Create your Function and try this:



                     public void saveText(String key, String value) {
                  SharedPreferences sPref = context.getSharedPreferences(config.PREFERENCES, context.MODE_PRIVATE);
                  SharedPreferences.Editor ed = sPref.edit();
                  ed.putString(key, value);
                  ed.commit();
                  ed.apply();
                  toast("Yatda saklandy");
                  }

                  public String loadText(String key) {
                  SharedPreferences sPref = context.getSharedPreferences(config.PREFERENCES, context.MODE_PRIVATE);
                  String savedText = sPref.getString(key, "");
                  return savedText;
                  }


                  and use it:



                  saveText("name",username.getText().toString());

                  loadText("name");





                  share|improve this answer


























                    0












                    0








                    0







                    Create your Function and try this:



                       public void saveText(String key, String value) {
                    SharedPreferences sPref = context.getSharedPreferences(config.PREFERENCES, context.MODE_PRIVATE);
                    SharedPreferences.Editor ed = sPref.edit();
                    ed.putString(key, value);
                    ed.commit();
                    ed.apply();
                    toast("Yatda saklandy");
                    }

                    public String loadText(String key) {
                    SharedPreferences sPref = context.getSharedPreferences(config.PREFERENCES, context.MODE_PRIVATE);
                    String savedText = sPref.getString(key, "");
                    return savedText;
                    }


                    and use it:



                    saveText("name",username.getText().toString());

                    loadText("name");





                    share|improve this answer













                    Create your Function and try this:



                       public void saveText(String key, String value) {
                    SharedPreferences sPref = context.getSharedPreferences(config.PREFERENCES, context.MODE_PRIVATE);
                    SharedPreferences.Editor ed = sPref.edit();
                    ed.putString(key, value);
                    ed.commit();
                    ed.apply();
                    toast("Yatda saklandy");
                    }

                    public String loadText(String key) {
                    SharedPreferences sPref = context.getSharedPreferences(config.PREFERENCES, context.MODE_PRIVATE);
                    String savedText = sPref.getString(key, "");
                    return savedText;
                    }


                    and use it:



                    saveText("name",username.getText().toString());

                    loadText("name");






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 20 '18 at 18:30









                    MukamMukam

                    314




                    314























                        0














                        In your first activity you are not initialising sharedPreference
                        To initialise do this



                        sharedPreferences = getSharedPreferences(prefrence , Context.MODE_PRIVATE);


                        Consider using apply().



                        It writes the changes to the RAM immediately and waits and writes it to the internal storage(the actual preference file) after. Commit writes the changes synchronously and directly to the file.






                        share|improve this answer




























                          0














                          In your first activity you are not initialising sharedPreference
                          To initialise do this



                          sharedPreferences = getSharedPreferences(prefrence , Context.MODE_PRIVATE);


                          Consider using apply().



                          It writes the changes to the RAM immediately and waits and writes it to the internal storage(the actual preference file) after. Commit writes the changes synchronously and directly to the file.






                          share|improve this answer


























                            0












                            0








                            0







                            In your first activity you are not initialising sharedPreference
                            To initialise do this



                            sharedPreferences = getSharedPreferences(prefrence , Context.MODE_PRIVATE);


                            Consider using apply().



                            It writes the changes to the RAM immediately and waits and writes it to the internal storage(the actual preference file) after. Commit writes the changes synchronously and directly to the file.






                            share|improve this answer













                            In your first activity you are not initialising sharedPreference
                            To initialise do this



                            sharedPreferences = getSharedPreferences(prefrence , Context.MODE_PRIVATE);


                            Consider using apply().



                            It writes the changes to the RAM immediately and waits and writes it to the internal storage(the actual preference file) after. Commit writes the changes synchronously and directly to the file.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 20 '18 at 18:49









                            Lekr0Lekr0

                            357212




                            357212






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399028%2fretrieve-data-using-sharedpreferences%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Guess what letter conforming each word

                                Port of Spain

                                Run scheduled task as local user group (not BUILTIN)