亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

android定義全局變量

系統(tǒng) 2255 0

The more general problem you are encountering is how to save stateacross several Activities and all parts of your application. A staticvariable (for instance, a singleton) is a common Java way of achievingthis. I have found however, that a more elegant way in Android is toassociate your state with the Application context.?

--如想在整個(gè)應(yīng)用中使用,在java中一般是使用靜態(tài)變量,而在android中有個(gè)更優(yōu)雅的方式是使用Application context。?

As you know, each Activity is also a Context, which is informationabout its execution environment in the broadest sense. Your applicationalso has a context, and Android guarantees that it will exist as asingle instance across your application.?
--每個(gè)Activity 都是Context,其包含了其運(yùn)行時(shí)的一些狀態(tài),android保證了其是single instance的。?

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 andmake it available for your entire application. You can access it fromany context using the Context.getApplicationContext() method (Activityalso provides a method getApplication() which has the exact sameeffect):?
--方法是創(chuàng)建一個(gè)屬于你自己的android.app.Application的子類,然后在manifest中申明一下這個(gè)類,這是android就為此建立一個(gè)全局可用的實(shí)例,你可以在其他任何地方使用Context.getApplicationContext()方法獲取這個(gè)實(shí)例,進(jìn)而獲取其中的狀態(tài)(變量)。?

給個(gè)例子:?

Java代碼?? 收藏代碼
  1. class ?MyApp? extends ?Application?{??
  2. ??
  3. ?? private ?String?myState;??
  4. ??
  5. ?? public ?String?getState(){??
  6. ???? return ?myState;??
  7. ??}??
  8. ?? public ? void ?setState(String?s){??
  9. ????myState?=?s;??
  10. ??}??
  11. }??
  12. ??
  13. class ?Blah? extends ?Activity?{??
  14. ??
  15. ?? @Override ??
  16. ?? public ? void ?onCreate(Bundle?b){??
  17. ????...??
  18. ????MyApp?appState?=?((MyApp)getApplicationContext());??
  19. ????String?state?=?appState.getState();??
  20. ????...??
  21. ??}??
  22. }??


This has essentially the same effect as using a static variable orsingleton, but integrates quite well into the existing Androidframework. Note that this will not work across processes (should yourapp be one of the rare ones that has multiple processes).?
--這個(gè)效果就是使用靜態(tài)變量是一樣的,但是其更符合android的架構(gòu)體系。

android定義全局變量


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 成年男女免费视频观看性 | 亚洲精品短视频 | 色拍自拍亚洲综合在线 | 欧美又粗又硬又大久久久 | 久久黑丝| 亚洲免费视频一区 | 一级特黄性色生活片一区二区 | 视频二区欧美 | 成人免费视频一区 | 成年女人在线观看 | 丁香激情综合色伊人久久 | 日本四虎影视 | 久久免费在线视频 | 日韩亚洲国产综合久久久 | 99精品国产自产在线观看 | 亚洲精品亚洲人成人网 | 国产精品久久久久久吹潮 | 久久国产美女 | 香蕉精品视频在线观看入口 | 国产日本欧美在线观看 | 色综合久久综合欧美综合 | 精彩视频一区二区三区 | 国产热re99久久6国产精品 | 99视频全部免费 | 久久午夜综合久久 | 亚洲精品美女一区二区三区乱码 | www.深夜| 日本特黄在线观看免费 | 日日摸夜夜摸人人嗷嗷叫 | 四虎免费观看 | 国产一区二区三区在线观看免费 | 蕾丝视频在线看片国产 | 四虎在线影视 | 欧美亚洲国产一区二区三区 | 久久久精品免费视频 | 999精品免费视频 | 91久久老司机福利精品网 | 久久99国产综合精品 | 日韩欧美在线观看 | 曰本还a大片免费无播放器 曰本黄色录像 | 欧美日韩亚洲国产精品一区二区 |