How to load the application according to the user id?

1

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

And in the other screens also, how would I save this user and use in the screens I have of expense / revenue listing and expense / revenue insertion?

I've been warned about such a Singleton , I'd like to see if it's also possible to use it.

    
asked by anonymous 10.03.2015 / 17:38

1 answer

1

Dude if you're saving the user to a table. when you start the app get your user in the table, and put it in the application. So from any screen you can access your user.

Follow the example

No Manifest

<application
        android:name="pacote.MyApplication"

Application Class

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

Your 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 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;

I hope I have helped with your knowledge ... and in your app ...

valew

    
11.03.2015 / 18:16