0

I am making an android app. I have a User class which has many getters and setters. I have many activities in my app and by just creating a single object of User class I want to access that object from many activities. How can I do that?


Thank you for the answer. But the scenario is like this. I have a database and in there is a table user which contains all the information about the user registration like name,age, password, emailID, last logged in etc. Each of these fields are there in the user class. Now my first activity is for accepting the terms and conditions page. If the user accepts it then I want to update that particular column in the database table. Then in next activity user registration will happen. Then I need to store that data in the database table. So in the same row all that data has to be inserted in the database. How will this be done

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
Pari Sharma
  • 11
  • 1
  • 2

1 Answers1

1

Try using singleton:

class User {
    private static User sInstance;

    private User() {
    }

    public static User getInstance() {
        if(sInstance == null) {
            sInstance = new User();
        }

        return sInstance;
    }

    // ... other methods
}

In activities:

User.getInstance().doSomething();

More here: https://stackoverflow.com/a/16518088/1979756

UPDATE: If you are using db, look at SQLiteOpenHelper. Extend it and manage all data there. Also, you can use some libs to manage your db data: ormLite, greenDAO, realm, etc. You can find a lot of info about them on SO.

Community
  • 1
  • 1
lewkka
  • 1,019
  • 15
  • 25