0

I have 2 activities namely MainActivity and OKActivity. The MainActivity statically checks for a password and lets you go to the OKActivity. I have used SharedPrefrences in the OKActivity for changing the password to a new one.

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText password = (EditText) findViewById(R.id.editText_Password);
    Button enter = (Button) findViewById(R.id.button);

    enter.setOnClickListener(new OnClickListener() {




        @Override
        public void onClick(View arg0) {

            String user_pass;
            user_pass = password.getText().toString();

                if (user_pass.isEmpty()) {
                    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
                        dialogBuilder.setIcon(R.drawable.ic_launcher);
                        dialogBuilder.setTitle("Oops!");
                        dialogBuilder.setMessage("Password Field Cannot Be Empty");
                        dialogBuilder.setPositiveButton("OK", null);
                        dialogBuilder.show();
                    }
                else
                    if (user_pass.equals("123")) {
                    Toast.makeText(MainActivity.this, "Welcome!", Toast.LENGTH_SHORT).show();
                    Intent I = new Intent("com.mavenmaverick.password.OKActivity");
                    startActivity(I);
                    }
                else
                        if(user_pass != ("123")){
                        Toast.makeText(MainActivity.this, "Incorrect", Toast.LENGTH_SHORT).show();
                        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
                            dialogBuilder.setIcon(R.drawable.ic_launcher);
                            dialogBuilder.setTitle("Oops!");
                            dialogBuilder.setMessage("Incorrect Password");
                            dialogBuilder.setPositiveButton("OK", null);
                            dialogBuilder.show();
                    }

        }
    });

}



public class OKActivity extends Activity {

EditText newPassword;
String newUserPassword;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ok);

    newPassword = (EditText) findViewById(R.id.new_password);
    newUserPassword = newPassword.getText().toString();


    getpasswordSharedPreferences();

    Button changePassword = (Button) findViewById(R.id.button_change);

    changePassword.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            newUserPassword = newPassword.getText().toString();
            getpasswordSharedPreferences();
            setSharedPreferences();

        }
    });

}

private String getpasswordSharedPreferences() {

    SharedPreferences userPassword = getSharedPreferences("USER_PASSWORD", MODE_PRIVATE);
    String password = userPassword.getString("THE_PASSWORD", "123");
    return password;

}

private void setSharedPreferences() {
    SharedPreferences userPassword = getSharedPreferences("USER_PASSWORD", MODE_PRIVATE);
    SharedPreferences.Editor password_edior = userPassword.edit();
    password_edior.putString("THE_PASSWORD", newUserPassword);
    password_edior.commit();
    Toast.makeText(OKActivity.this, "Password Change Succesful", Toast.LENGTH_SHORT).show();
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(OKActivity.this);
        dialogBuilder.setIcon(R.drawable.ic_launcher);
        dialogBuilder.setTitle("Done!");
        dialogBuilder.setMessage("New Password : "+newUserPassword);
        dialogBuilder.setPositiveButton("OK", null);
        dialogBuilder.show();

}

How can I access the SharedPrefrences in OKActivity for the password and use it in my MainActivity to allow access thereby making things dynamic over user-interaction cycles.

  • what is the problem you are facing can you explain a bit – Pramod Yadav Jan 15 '15 at 11:57
  • 1
    @Maven Maverick you need to access SharedPrefrences in MainActivity .is that you need – sherin Jan 15 '15 at 12:01
  • 1
    hi, you can call getpasswordSharedPreferences() & setSharedPreferences() in your MainActivity also. But make sure its name "USER_PASSWORD" should be consistent. – droidd Jan 15 '15 at 12:02
  • refer to http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values for the solution of your problem – droidaddict Jan 15 '15 at 12:12

3 Answers3

0

You get the same way in both activity's. Do get of your SharedPreferences with the same TAG.

private String getpasswordSharedPreferences() {

    SharedPreferences userPassword = getSharedPreferences("USER_PASSWORD", MODE_PRIVATE);
    String password = userPassword.getString("THE_PASSWORD", "123");
    return password;

}

Maybe you can put this method's in other class, and call when you want from all activities: You can put your set method here too For example:

public class SharedPrefs {
   private static final String SHARED_PREF = "USER_PASSWORD";
   private static final String KEY_PASSWORD = "THE_PASSWORD";

   public static void getStoredSharedPref(Context context, String key, String value) {

    SharedPreferences sharedPref = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();

    editor.putString(propertyKey, value);
    editor.commit();
    }
 }

and then call in your activities

 SharedPrefs.getStoredSharedPref(context, SharedPrefsUtils.KEY_PASSWORD,"1234");
rafaelasguerra
  • 2,685
  • 5
  • 24
  • 56
0

Just access the SharedPreferences in your OKActivity and in your MainActivity. The trick is to use the same TAG name - in your case it's 'USER_PASSWORD'.

Have a look at this --> SharedPreferences

Kody
  • 1,154
  • 3
  • 14
  • 31
0

Create a SharedPrefrences.java //then we can use when ever we need

public class SharedPrefrences {

public static void saveData(String name, String value, Context context) {

    try {

        SharedPreferences settings = context
                .getSharedPreferences(Configuration.getPrefsName(), 0);

        SharedPreferences.Editor editor = settings.edit();

        editor.putString(name, value);
        editor.commit();
    } catch (NullPointerException ignored) {

    }

}

public static String getData(String name, Context context) {

    try {


        SharedPreferences settings = context
                .getSharedPreferences(Configuration.getPrefsName(), 0);

        return settings.getString(name, "");
    } catch (NullPointerException ignored) {
        return "";
    }

}

 }

//In MainActivity

   SharedPrefrences.saveData("Password","123456", getApplicationContext());

//In OKActivity

   String passwordfromMainActivty = PreferencesUtils.getData("Password", getApplicationContext());

//To Add Newpassword

     SharedPrefrences.saveData("NewPassword","abcd", getApplicationContext());
sherin
  • 1,091
  • 2
  • 17
  • 27