Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6795803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:23:26+00:00 2026-05-26T18:23:26+00:00

Hi i have a inlogscreen (inlogdialog.xml), which includes 2 EditText (username, passwd) and i

  • 0

Hi i have a inlogscreen (inlogdialog.xml), which includes 2 EditText (username, passwd) and i have a CheckBox (saveuser) with which the user can decide weather to save the username or not.

On the mainform (main.xml) i have a listner for these 3 values:

private class OnReadyListener implements MyCustomForm.ReadyListener {

    public void ready(String user, String pass, boolean save) {
        username = user;
        password = pass;
    }

}

Now i first want to save the username through SharedPreferences but it doesn`t get saved, can someone help me please?

If i check with system.out.println, the username is present in String username.

SharedPreferenes code in main.xml:

public static final String USERNM = ""; 
private SharedPreferences mPrefs; 
.......

@Override     
protected void onPause() {  
    Editor e = mPrefs.edit();   
    e.putString(USERNM, username); <----
    e.commit();     
    Toast.makeText(this, "Items saved.", Toast.LENGTH_SHORT).show();         
    super.onPause();    
}

Edit:

public static final String PREFS_NAME = "SharedPrefsDemoPreferences";  
public static final String PREF_BOOL = "Bool"; 
public static final String USERNM = "User"; 

private SharedPreferences mPrefs;
....
@Override 
protected void onResume() { 
    mPrefs = getSharedPreferences(PREFS_NAME, 0); 
    if(mPrefs!=null) 
        myBoxState=mPrefs.getBoolean(PREF_BOOL, false); 
    super.onResume(); 
}

Edit 2:

@Override     
protected void onPause() { 
    System.out.println("user: " + username); <---- value username is there
    Editor e = mPrefs.edit();   
    //e.putBoolean(PREF_BOOL, nieuwbel.isChecked()); 
    e.putString(USERNM, username);
    e.commit(); 
    Toast.makeText(this, "Instelling beltegoed opgeslagen.", Toast.LENGTH_SHORT).show();         
    super.onPause();    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:23:26+00:00Added an answer on May 26, 2026 at 6:23 pm

    Better not use empty key for the username. Saving data in SharedPreferences is based on saving a key-value pair. You retrieve the value by providing the key. Currently the key for your username is empty string.

    Try something like:

        //initialize some default values and keys for the SharedPreferences
    private static final String DEF_USERNAME_KEY = "prefsUsername";
    private static final String DEF_USERNAME_VALUE = "def_username_value";
    
    private static final String DEF_PASSWORD_KEY = "prefsPassword";
    private static final String DEF_PASSWORD_VALUE = "def_pass_value";
    
    private void saveUserData(String username, String password) {
    
        SharedPreferences.Editor userPrefsEditor = userPrefs.edit();
    
        userPrefsEditor.putString(DEF_USERNAME_KEY, username); 
        userPrefsEditor.putString(DEF_PASSWORD_KEY, password);
    
        userPrefsEditor.commit();
    
                //check if data is saved
        Log.d(TAG, "Getting from SharedPreferences " + DEF_USERNAME_KEY + " " + 
                userPrefs.getString(DEF_USERNAME_KEY, DEF_USERNAME_VALUE)); 
        Log.d(TAG, "Getting from SharedPreferences " + DEF_PASSWORD_KEY + " " + 
                userPrefs.getString(DEF_PASSWORD_KEY, DEF_PASSWORD_VALUE)); 
    }
    

    Edit: This is how you retrieve from SharedPreferences:

    userPrefs.getString(DEF_USERNAME_KEY, DEF_USERNAME_VALUE)
    

    IN your case this will look like:

    String returnedUsername = e.getString(USERNM, "");
    

    than returnedUsername holds the value you stored in SharedPrefs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have just started using Visual Studio Professional's built-in unit testing features, which as I
have anyone can tell me what syntax error on this actionscript (actionscript3.0)? var rotY:
Have a linking (or ref) table which has a dual primary key. Need to
Have a xml string, goal is to replace an xml element value to a
Have anyone used Redmine Documentor which lets you convert PHP to HTML to Redmine
Have the following scenario. I have a few form, which essentially have a few
Have an app that can use tts to read text messages. It can also
Have such a problem, hope you'll help me.. Can't find anywhere. Here is the
Have a bunch of classes which I need to do serialize and deserialize from/to
Have you ever tried working with a XAML file which contains thousand tons of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.