开发者

Android Widget Application State

开发者 https://www.devze.com 2023-02-08 14:37 出处:网络
I have a Android Widget, Configuration Activities, and a heavily used Android Service full of functions.I want to maintain global application state/status that can be referenced from any of the above

I have a Android Widget, Configuration Activities, and a heavily used Android Service full of functions. I want to maintain global application state/status that can be referenced from any of the above locations. The state I am referring to is application domain speci开发者_如何学运维fic status. For example STARTED, LEVEL1, LEVEL2 etc. So I would like to know the following:

1) What are the advantages of Global Singleton for keeping this state/status vs. subclassing the Android Application and using it as the Singleton?

2) I want the state to be a recoverable singleton. So I need to save it off when the App gets shutdown, process terminated. Where is the right place to save off the overall application state? the terminate method on Application can be overridden but its not really guaranteed to get called. So I am looking also for the point at which to save off the application state. Its not clear to me. The Activity is not the global application, neither is the widget nor the service, so where can I tell that the application is being shutdown/terminated and saveoff the application state.

3) Is their anything wrong with initializing the state in Application.onStart()?


Why not use the Service? That is a good place to keep live state. AFAIK it is guaranteed that Service is a singleton, and onCreate() and/or onStartCommand() offer places appropriate for initializations. I'm unclear any guarantee that onDestroy() gets called, but the docs seem to say it will:

"A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the Context.BIND_AUTO_CREATE flag. Once neither of these situations hold, the service's onDestroy() method is called and the service is effectively terminated." ref


1) What are the advantages of Global Singleton for keeping this state/status vs. subclassing the Android Application and using it as the Singleton?

If by "Global Singleton" you mean a static data member, there are no advantages or disadvantages of significance either way, IMHO.

2) I want the state to be a recoverable singleton. So I need to save it off when the App gets shutdown, process terminated. Where is the right place to save off the overall application state?

Every time it changes, more or less.

The Activity is not the global application, neither is the widget nor the service, so where can I tell that the application is being shutdown/terminated and saveoff the application state.

You aren't told. Hence, you save it every time it changes. Your static data member, Application object, or whatever is a cache for persistent data -- everything else is subject to utter destruction. (insert evil maniacal laugh here)

3) Is their anything wrong with initializing the state in Application.onCreate()? (corrected)

I assume you mean onCreate(). You are on the main application thread, and so I/O might be kicked off in onStart() but should occur on an AsyncTask or other background thread.

0

精彩评论

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