How to create an ID to be used in other classes

0

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 ?

    
asked by anonymous 24.07.2017 / 15:11

2 answers

1

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);
    
24.07.2017 / 15:18
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();
    
24.07.2017 / 15:41