可能重复:
Android中的单身人士与应用程序上下文?
我不太明白这个:
当您的应用程序实现在清单中注册时,它将在创建应用程序进程时实例化.因此,您的应用程序实现本质上是一个单例,应该这样实现,以提供对其方法和成员变量的访问.
public class MyApplication extends Application {
private static MyApplication singleton;
// Returns the application instance
public static MyApplication getInstance() {
return singleton;
}
public final void onCreate() {
super.onCreate(); singleton = this;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在阅读这是推荐的...但我的应用程序对象MyApp扩展应用程序工作正常...我没有这样的代码.
我只是通过强制转换getApplication()来获取MyApp对象
有人可以解释实现这样的代码的好处吗?我错过了一些明显的东西吗?
| 归档时间: |
|
| 查看次数: |
18256 次 |
| 最近记录: |