pmb
pmb

Reputation: 2327

static singleton class becoming null

I read a lot but couldn't understand how to restore state when some data kept on singletone class. for example

public class UserDataKeeper {


    private static UserDataKeeper instance;

    private User mUser;

    private UserDataKeeper(){
    }

    public static UserDataKeeper getInstance() {

        if (instance == null) {
            instance = new UserDataKeeper();
        }

        return instance;
    }

    public User getUser() {
        return mUser;
    }

    public void setUser(User mUser) {
        this.mUser = mUser;
    }
}

When my android application come from background user data becoming null. what should I do here for not getting null result.

Is the only solution is save data using SQLite,Preference or something else ?

Upvotes: 0

Views: 685

Answers (1)

Egor
Egor

Reputation: 40193

Even if you're implementing a Singleton (which is almost always a bad solution), you're still storing the data in memory. After your application is closed the data will be lost. If you want persistent storage then yes, you'll need to have either a SQLite database, or write your information to disk.

Upvotes: 1

Related Questions