So I just started out with JAVA and I have been trying to save a simple combination of username and password into SharedPreferences. Though i'm not so sure I understand how this actually works. So here is the code I currently have;
public void init() {
EditText editText = (EditText) findViewById(R.id.password);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
EditText passwordText = (EditText) findViewById(R.id.password);
String Password = passwordText.getText().toString();
EditText usernameText = (EditText) findViewById(R.id.username);
String Username = usernameText.getText().toString();
SaveData(Username, Password);
handled = true;
}
return handled;
}
});
}
private void SaveData(String Username, String Password) {
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(getString(R.string.saved_high_score), newHighScore);
editor.commit();
}
Now of course this will not work since I don't have any getActivity(), but I'm wondering what that's supposed to be? Or else how I could save the already available data that i'm sending to the function.
My ideal result would be to be able to get the username and password from another activity. Now I rather not use local files for this but stick with the SharedPreferences.
Much thanks in advance!