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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:22:56+00:00 2026-06-15T17:22:56+00:00

sir, how do access my sharedpreferences file in other class? i want the application

  • 0

sir, how do access my sharedpreferences file in other class? i want the application to have a default password of 1234, then change it if the user wants to. i’ve used this code and it works for the first time, but by the time i restart the application, it will revert back to the old password. thanks for help in advance

public class ChangePassword extends Activity {
/** Called when the activity is first created. */
EditText enterPassword, newPassword;
public final String filename = "PasswordFile";
String myPassword;
SharedPreferences myFolder;
public static String dataReturned = "";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.changepassword);
    Button save = (Button)findViewById(R.id.btnSavePassword);
    enterPassword = (EditText)findViewById(R.id.etCurrentPassword);
    newPassword = (EditText)findViewById(R.id.etNewPassword);
    myFolder = getSharedPreferences(filename, 0);//a folder
    dataReturned = myFolder.getString("passwordKey", "");
    if(dataReturned.equals(""))
    {
        SharedPreferences.Editor editor = myFolder.edit();
        editor.putString("passwordKey", "1234"); //newData is new pass, passwordKey is key
        editor.commit();
    }

    save.setOnClickListener(new OnClickListener()
    {
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String stringData = enterPassword.getText().toString().trim();
            dataReturned = myFolder.getString("passwordKey", ""); //key/def message
            //if stored password is equal to entered password
            if(dataReturned.equals(stringData))
            {
                String newData = newPassword.getText().toString().trim();
                SharedPreferences.Editor editor = myFolder.edit();
                editor.putString("passwordKey", newData); //newData is new pass, passwordKey is key
                editor.commit();
                dataReturned  = myFolder.getString("passwordKey", "couldn't load data"); //get file from sharedpref
                Toast.makeText(getApplicationContext(),
                        "Password successfully changed",
                        Toast.LENGTH_LONG).show();
                enterPassword.setText("");
                newPassword.setText("");
            }
            else 
            {
                Toast.makeText(getApplicationContext(),
                        "Wrong password!",
                        Toast.LENGTH_LONG).show();
                enterPassword.setText("");
                newPassword.setText("");
            }
        }
    });
}//oncreate()

}

here is where i want to use the data saved in sharedpreferences in my other class

public void dispatch_button_action(View view){
    final EditText password_input = new EditText(this); // create an text input field
    password_input.setHint("Enter Password"); // put a hint in it
    password_input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); // change it to password type

    AlertDialog.Builder alertDialog = new Builder(this); // create an alert box
    alertDialog.setTitle("Enter Password"); // set the title
    alertDialog.setView(password_input);  // insert the password text field in the alert box
    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // define the 'OK' button
        public void onClick(DialogInterface dialog, int which) {
             String entered_password = password_input.getText().toString();
             String password = ChangePassword.myFolder.getString("passwordKey", "");
             if(!password.trim().equals(""))
             {
                 testPassAndSendSms(password.trim(), entered_password);
             }
             else
             {
                 testPassAndSendSms(my_password, entered_password);
             }

        } 
    });
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { // define the 'Cancel' button
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        } 
    });
    alertDialog.show(); // show the alert box
}//end dispatch_button
  • 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-06-15T17:22:57+00:00Added an answer on June 15, 2026 at 5:22 pm

    Currently you are not saving the latest password entered by user in Alertbox so in Another Activity save latest password in SharedPreferences as :

     alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) {
       String entered_password = password_input.getText().toString();
       SharedPreferences myFolder = You_Current_Activitu.this.getSharedPreferences(filename, 0);
            String  dataReturned = myFolder.getString("passwordKey", "");
            if(dataReturned.equals(""))
            {
                SharedPreferences.Editor editor = myFolder.edit();
                editor.putString("passwordKey", entered_password); 
                editor.commit();
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sir, i am working in a java application.On that application i have to access
Respected sir/madam, I have a fortran exe which takes a input file and produces
Sir/Madam, I want to know where WSDL exists. Do user need to do interact
I have this cod: [Serializable] public class testC { string sir; public testC() {
sir, how do i save the changes made in my change password activity if
I have a Forgot Password TextView on which I performed onPressed() event and want
Sir, I have two window forms, each form has some controls. I want to
Sir, I am doing the task on fetching the contents from the xml file.I
Hello Sir i want send list of data to php server i use following
I need to write something into a text file's beginning. I have a text

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.