How to create an ID to be used in other classes

Asked

Viewed 161 times

0

My app will get to the first screen of the user ID, how do I use it in other activities unseen intent ?

2 answers

1


You can store in a Shared Preferences, although I’m not sure it’s 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);

1

Create a class to set and take 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 catch it.

                User usuario = ((User)getApplicationContext());
            usuario.setId(id);//variável com o id

To recover (in the example, displays the set variable in a snackbar):

        User usuario = ((User)getApplicationContext());
        Snackbar snackbar = Snackbar
                .make(principal, "Bem Vindo, " + usuario.getId(), Snackbar.LENGTH_LONG);
        snackbar.show();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.