What is the simplest way in Android to keep an obj

2019-03-03 13:00发布

问题:

What is the simplest way to store a single object value so that the object is not null the next time the app is executed?

for example:

public class MainActivity extends Activity {
    ...
    String X = "Hello"
}

The next time I run this app, X will still be defined as "Hello".

回答1:

You need to look at Storage Options and see which is best for your needs. You probably want to add it to sharedprefs somewhere, which uses a key/value pair, if it is lite data. Otherwise, you may want a SQLite DB. But X is always going to be "Hello" the way you have it so you will need a function somewhere to set the sharedprefs or whatever you use. I'm assuming you just put that code for simplicity but just pointing that out.