Pass parameters between activities Android studio

0

I have the following problem, I have an application that has login, which uses username and rout to validate the entry. the variable rut I want to save it, to show it in another activity, which I choose in a menu within the application, in this activity I want to show the parameter of the rut in a textview or an editText.

What method can I use to save the parameter and send it to the class I open from the menu?

    
asked by Fernando Brito 07.06.2018 в 07:45
source

2 answers

1

If it is to save the data of a login, it is best to use the SharedPreferences of android, so keep the data in session:

//Cargas una nueva instancia de SharedPreferences y la pones un nombre
SharedPreferences prefs = this.getSharedPreferences("miApp", Context.MODE_PRIVATE);

//Asi guardas un dato (por ejemplo el rut y la id de usuario logueado)
prefs.edit().putString('rut', 'valorquequieras').apply();
prefs.edit().putInt('id', 1).apply();

//Asi recuperas un valor
String rut = prefs.getString('rut', 'valorpordefecto');
Int id = prefs.getInt('id', -1);

When entering a value you give it a name and assign it the value you want and to recover it you say what value you want to recover and as a second parameter a default value in case it does not exist, so you can do checks.

SharedPreferences are accessible from anywhere in the code. I hope it serves you

    
answered by 07.06.2018 в 08:15
0

I solved my problem, thanks for the comments, I took SharedPreferences and I will leave the code that was used, so that someone can reuse it or solve their problem.

// In this way I keep the value

SharedPreferences guardarRut = getBaseContext().getSharedPreferences("guardarRut", MODE_PRIVATE);
        Editor edit = guardarRut.edit();
        edit.putString("rut", rutuser.trim()).apply();
        edit.apply();

// So I recover the value

 SharedPreferences guardarRut = getBaseContext().getSharedPreferences("guardarRut", MODE_PRIVATE);
    String userName = guardarRut.getString("rut", "");
    rut.setText(userName);
    
answered by 07.06.2018 в 09:18