Singletons vs. Application Context in Android?

后端 未结 10 1424
醉话见心
醉话见心 2020-11-22 03:11

Recalling this post enumerating several problems of using singletons and having seen several examples of Android applications using singleton pattern, I wonder if it\'s a go

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 03:22

    Application is not the same as the Singleton.The reasons are:

    1. Application's method(such as onCreate) is called in the ui thread;
    2. singleton's method can be called in any thread;
    3. In the method "onCreate" of Application,you can instantiate Handler;
    4. If the singleton is executed in none-ui thread,you could not instantiate Handler;
    5. Application has the ability to manage the life cycle of the activities in the app.It has the method "registerActivityLifecycleCallbacks".But the singletons has not the ability.

提交回复
热议问题