0

I have an activity login with username and password, and second activity that show a list of record. I passed from first activity to second pushing the login button. In the second activity I have the back button.

How is possible to show the previously entered data, in the record of username and password (in the login activity) when the back button is pressed?

Onur A.
  • 3,007
  • 3
  • 22
  • 37

1 Answers1

0

You can use shared preference to save your information from the first screen like this :

   //save you SharedPreferences
SharedPreferences.Editor yourEditor = getSharedPreferences("pref", 
MODE_PRIVATE).edit();
yourEditor.putString("name", "user name");
yourEditor.putInt("password", 123);
yourEditor.apply();

//how to get them
SharedPreferences prefs = getSharedPreferences("pref, MODE_PRIVATE); 
String userName = prefs.getString("name", "default");
//default is the  default value
int idName = prefs.getInt("password", 0); //0 is the default value.
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • 2
    Saving plaintext password in a plaintext xml file sounds like a surefire way to introduce security vulnerabilities. – EpicPandaForce Mar 10 '19 at 13:45
  • I agree with you at 100%, the solution i sujested is not secured but from the question it seems like he is not using any database and just want to pass back the data to the first screen, and this is the reason that i answered him as i did. – Tamir Abutbul Mar 10 '19 at 13:51
  • 1
    Why not just use `setResult()` and `startActivityForResult()`, then? – EpicPandaForce Mar 10 '19 at 14:02
  • 1
    No reason actually, now that you have said it i is really a better solution than what i wrote in my answer.This is a good solution - not sure why this was not my first thing that came into my mind.Thank you anyway for reminding me that simple solution. – Tamir Abutbul Mar 10 '19 at 14:10