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

  • Home
  • SEARCH
  • 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 6252779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:52:05+00:00 2026-05-24T13:52:05+00:00

I save a user’s username and passwords the first time he opens the app

  • 0

I save a user’s username and passwords the first time he opens the app and store it in a SharedPreferences object. I check for the data the second time he enters and if its not null, then I got into the app. Here is how I’m doing this:

private SharedPreferences dhj;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dhj = this.getSharedPreferences("DHJ", MODE_WORLD_READABLE);
    if(dhj.getString("username", null) != null) {
        setContentView(R.layout.main);
            // do some stuff...
    }
    else {
            setContentView(R.layout.login);
            username = (EditText) findViewById(R.id.username);
            password = (EditText) findViewById(R.id.password);
                    loginButton = (Button) findViewById(R.id.loginButton);

            loginButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    SharedPreferences.Editor dhjEditor = dhj.edit();
                    dhjEditor.putString("username", username.getText().toString());
                    dhjEditor.putString("password", password.getText().toString());
                    setContentView(R.layout.main);
                }
            }); 
                    // do some other stuff...
    }
}

But each time I open the app, I am being asked to enter the username and password.
What am I doing wrong? How can I achieve the desired functionality?
Thank you.

  • 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-24T13:52:06+00:00Added an answer on May 24, 2026 at 1:52 pm

    You need to call the Editor’s commit method after making any change to preferences. This will save the preferences file:

    SharedPreferences.Editor dhjEditor = dhj.edit();
    dhjEditor.putString("username", username.getText().toString());
    dhjEditor.putString("password", password.getText().toString());
    dhjEditor.commit();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having hard time trying to figure out how to auto-save user data in
I'm developing web app that user can save his/her work to server. The data
My app uses XML to save user data to a file. I have just
What is the best way to save a user profile with Google App Engine
In my app, the user can save his input to disk. This is done
I would like to save data in cookies (user name, email address, etc...) but
I would like to save the programs settings every time the user exits the
What is the common approach to save user's inputed data: 1. Overriding onPause() method
In my linux python app for Fedora I want to save user's work to
How can save data into a XML file without user prompt dialog in Action

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.