How do I load the application as user id?

Asked

Viewed 144 times

1

In my app you have a user registration. Each user has a name and email, I would like when you start the application to save the default user id (or reload the application when I change the user) because I would fetch the information from the database according to the key id_usuario and list from that attribute. An example:

Tela principal

And on the other screens too, I would like to save this user and use on the screens I have of expense/revenue listing and expense/revenue insertion?

I was warned about a certain Singleton, I would like to see if it is possible to use it too.

  • where are you storing these users? on mobile or cloud?

  • on the same phone, I made a table and put it there, very simple. @dariodm

  • certro lets answer!

  • let who answer? @dariodm

1 answer

1


Face if you are saving the user in a table. when you start the app grab your user from the table, and put them in the Application. So from any screen you can access your user.

Follow the example

in the manifest

<application
        android:name="pacote.MyApplication"

Application class

public class MyApplication extends Application {
private User user;
...gets sets

its main Actv

public class MainActivity extends Activity {

private MyApplication application;
private User user;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

application = (MyApplication) this.getApplicationContext();

user = //pego o user em seu db sqlite
application.setUser(user); // set o user no seu application
//agora pode usar o objeto que e esta no Apllication
// Ex: application.getUser().getNome;

using in another Actv

public class OutraActivity extends Activity {
private MyApplication application;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_outra_tela);

application = (MyApplication) this.getApplicationContext();
//use o objeto do Application
application.getUser().getNome;

hope to have helped in your knowledge...and in your app...

Valew

  • thanks for the reply! And when I want to change users? i have an option on the main screen to change user, in it I open a dialog with a spinner that gives me the names of users, how do I reload the app with the user I just chose? @Flipnovid

  • set the Application user in your dialog, with the spinner click, but then you need to put a callback to when clicking on Activity. I think, it’s not sure, that when you open a dialog and go back to Actv it enters the onresume, more if you enter the on renume of Actv you arrow in it the components with the attributes of your new user. OK... Valew

  • I’m trying to use the application = (Myapplication) this.getApplicationContext(); in a sqliteopenhelper class and I don’t have this getapplicationcontext... how should I proceed?

  • Are you in an Actv, Frag or Fragactv? Sure you didn’t skip a step... ? @Allan Chrystian

  • 'application = ((Myapplication) this.getApplicationContext();' so try

Browser other questions tagged

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