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 7083733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:12:52+00:00 2026-05-28T07:12:52+00:00

I want to save a String value on Android and have access to this

  • 0

I want to save a String value on Android and have access to this String every time the application starts.

For instance the String value will have the user’s name which he has created on his own. And after restarting the app he would have this name already on the top. Like a cookie or something. How to save such file on android memory ?

Can someone guide me?

I used:

public class Login extends Activity
{
String user = null;

public String saveUserOnAndroid()
{
    SharedPreferences myPrefs = getApplicationContext().getSharedPreferences("myPrefs", 0);

    String savedUser = myPrefs.getString("user", null);

    if(savedUser == null)
    {
        user = UUID.randomUUID().toString();
        String hashedUser = md5(user);
        SharedPreferences.Editor myPrefsEditor = myPrefs.edit();

        myPrefsEditor.putString("user", hashedUser);
        myPrefsEditor.commit();

        return hashedUser;
    }   
    else
        return savedUser;
}

and it seems not to work well.

  • 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-28T07:12:53+00:00Added an answer on May 28, 2026 at 7:12 am

    Well you didn’t mention you’re calling the saveUserOnAndroid() method from another class. In this case:

    public String saveUserOnAndroid(Context c) {
      SharedPreferences myPrefs = c.getSharedPreferences("myPrefs", 0);
      String savedUser = myPrefs.getString("user", null);
    
      if(savedUser == null) {
        user = UUID.randomUUID().toString();
        String hashedUser = md5(user);
        SharedPreferences.Editor myPrefsEditor = myPrefs.edit();
    
        myPrefsEditor.putString("user", hashedUser);
        myPrefsEditor.commit();
    
        return hashedUser;
      } else {
        return savedUser;
      }
    }
    

    And when calling this method, don’t forget to supply it the required Context and you should be okay.

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

Sidebar

Related Questions

I want to save a set of key, value pairs (string, int) between runs
For example, in this controller: @RequestMapping(value = /save, method = RequestMethod.POST) public @ResponseBody String
i want to save a balance of user, in the user table . the
Here is the parent question: save string to file I want to pass the
I have an object, say a note, whose parameters I want to save in
I have a problem with a string value. The string is globally declared. The
I want to add both String and Integer type of value in ArrayList. For
I have method that transforms some input value by the user passing it a
In my application I have a login page were the user can log in.
Possible Duplicate: Android: Saving a state during Android lifecycle i want to save some

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.