为什么要扩展Android Application类? [英] Why extend the Android Application class?

查看:91
本文介绍了为什么要扩展Android Application类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

扩展的Application类可以声明全局变量.还有其他原因吗?

An extended Application class can declare global variables. Are there other reasons?

推荐答案

我不认为真正的场景是,扩展Application要么比另一种方法更可取,要么需要完成某件事.如果您有一个昂贵且经常使用的对象,则可以在检测到该对象当前不存在时在IntentService中对其进行初始化.应用程序本身在UI线程上运行,而IntentService在其自己的线程上运行.

Offhand, I can't think of a real scenario in which extending Application is either preferable to another approach or necessary to accomplish something. If you have an expensive, frequently used object you can initialize it in an IntentService when you detect that the object isn't currently present. Application itself runs on the UI thread, while IntentService runs on its own thread.

我更喜欢使用显式的Intent将数据从Activity传递到Activity,或使用SharedPreferences.还有一些方法可以使用接口将数据从Fragment传递到其父Activity.

I prefer to pass data from Activity to Activity with explicit Intents, or use SharedPreferences. There are also ways to pass data from a Fragment to its parent Activity using interfaces.

这篇关于为什么要扩展Android Application类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆