My application will do a get on the first screen of the user ID, how do I use it in other activities
without being via intent
?
My application will do a get on the first screen of the user ID, how do I use it in other activities
without being via intent
?
You can store in a Shared Preferences, although I'm not sure which is the best option:
// Inicializar
SharedPreferences sharedPreferences = getSharedPreferences("shared_pref_key", Context.MODE_PRIVATE);
// Save Data
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("id", 1);
editor.commit();
//Pegar valor
int myIntValue = sharedPreferences.getInt("your_int_key", -1);
Create a class to set and get the value:
public class User extends Application {
private String Id;
public String getId()
{
return Id;
}
public void setId(String Id)
{
this.Id = Id;
}
}
And arrow the id when you first get it.
User usuario = ((User)getApplicationContext());
usuario.setId(id);//variável com o id
To recover (in the example, it displays the variable set in a snackbar):
User usuario = ((User)getApplicationContext());
Snackbar snackbar = Snackbar
.make(principal, "Bem Vindo, " + usuario.getId(), Snackbar.LENGTH_LONG);
snackbar.show();