Android应用程序中,关于保存全局数据是使用Singletons还是Application对象的一些讨论:

(注明都是网上和书中摘录,为了好看的绿色,妄称原创,见谅!)

Singletons are a nightmare for testing and, if lazily initialized, will introduce "state indeterminism" with subtle side effects (which may suddenly surface when moving calls to getInstance() from one scope to another). Visibility has been mentioned as another problem, and since singletons imply "global" (= random) access to shared state, subtle bugs may arise when not properly synchronized in concurrent applications.
I consider it an anti-pattern, it's a bad object-oriented style often embraced by people coming from procedural languages like C, where it is common to maintain global state.

To come back to your question: Although the app context can be considered a singleton itself, it is framework-managed and has a well defined life-cycle, scope, and access path. Hence I believe that if you do need to manage app-global state, it should go here, nowhere else. For anything else, rethink if you really need a singleton object, or if it would also be possible to rewrite your singleton class to instead instantiate small, short-lived objects that perform the task at hand.

Android SDK关于Application类介绍提到的:
There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally usesContext.getApplicationContext() when first constructing the singleton.

而《Android in Practice》书中介绍的:
We’ll talk more about lifecycle in chapter 3, but the important thing to keep in mind with the Application object is that it’s created when the process for your application is created, and it isn’t bound to a particular Activity or Service. This means it’s a great and extremely simple way to hold onto and share nontrivial and nonpersistent data between activities and services within your application. By nontrivial and nonpersistent, we mean data that your application needs which would be cumbersome to pass around as Intent extras everywhere, and also isn’t appropriate for a file or database.

ANOTHER WAY TO SHARE IN-APP DATA Another good choice for nontrivial and nonpersistent data is a static singleton object. You have to be careful with statics, though. They don’t have a well-defined lifecycle, and it’s easy to hang onto a reference that could cause a memory leak. If you prefer statics over the Application object, that’s fine, but consider setting up and tearing down your static classes from the Application object, which does have a welldefined lifecycle, for the best of both worlds.

下面是一个使用Application的例子
The more general problem you are encountering is how to save state across several Activities and all parts of your application. A static variable (for instance, a singleton) is a common Java way of achieving this. I have found however, that a more elegant way in Android is to associate your state with the Application context.

As you know, each Activity is also a Context, which is information about its execution environment in the broadest sense. Your application also has a context, and Android guarantees that it will exist as a single instance across your application.

The way to do this is to create your own subclass of android.app.Application, and then specify that class in the application tag in your manifest. Now Android will automatically create an instance of that class and make it available for your entire application. You can access it from any context using the Context.getApplicationContext() method (Activity also provides a method getApplication() which has the exact same effect):
class MyApp extends Application {  private String myState;  public String getState(){    return myState;  }  public void setState(String s){    myState = s;  }}class Blah extends Activity {  @Override  public void onCreate(Bundle b){    ...    MyApp appState = ((MyApp)getApplicationContext());    String state = appState.getState();    ...  }}
This has essentially the same effect as using a static variable or singleton, but integrates quite well into the existing Android framework. Note that this will not work across processes (should your app be one of the rare ones that has multiple processes).

更多相关文章

  1. Android(安卓)project 的常用编译方法
  2. Android获取Contact Number的例子(2.0系统以前的获取方式)
  3. 基于Android的WebService开发例子
  4. Android(安卓)关于没有处理异常导致应用程序崩溃的处理
  5. 设备管理应用程序的实现(Device Administration )
  6. Learning Android(安卓)Studio to build a app from scratch
  7. Android(安卓)使用 Application 总结
  8. (原创)在Android(安卓)上运行 openCV ,并做灰度变化的一个例子
  9. Android开发学习1 - Android架构

随机推荐

  1. 在Android上使用Http客户端的选择(译文)
  2. Android学习系列(40)--Android主题和样式
  3. Android学习之使用RadioGroup与RadioButt
  4. Android统计EditText的字母数字以及汉字
  5. Android(安卓)Camera2拍照(一)——使用Surf
  6. Android实现数据存储技术
  7. Android(安卓)技术专题系列之五 -- 本地化
  8. Android(安卓)ImageView的scaleType属性
  9. android 三级联动开源框架用法
  10. Android学习笔记(29):搜索框SearchView