开发者

Android - first run preference initialization

开发者 https://www.devze.com 2022-12-20 08:53 出处:网络
I have next s开发者_运维问答ituation: After installing application it has to init settings from server. And while itis not has right preferences it has to wait. I have several receivers and service -

I have next s开发者_运维问答ituation: After installing application it has to init settings from server. And while itis not has right preferences it has to wait. I have several receivers and service - I dont want to check for initialization in every action in these components - I think there have to be better solution. Can somebody help me with it? :)


You can have a static class that holds all of the initialization data and it can also hold a boolean for if the values are initialized.

You could also have a class that extends Application and do the initialization in that, so that way you can guarantee that it happens before anything else needs it.


Create a class which extends Application. Initialize your application inside onCreate() method, you can fill some fields of your class.

Then, from any receivers, services or activities, you can retrieve your application class with :

/* OnCreate method of MyApplication is executed at startup */
MyApplication myApp = (MyApplication) getApplication();
...
/* you can add any method to retrieve an initialized field */
myApp.getField() 


You could also explore the SharedPreferences object. It provides the features you seek. You could have key-value pairs that you populate from your server call and in your code you just check if the preference value is what you expect.

0

精彩评论

暂无评论...
验证码 换一张
取 消