<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    冰浪

    哥已不再年輕 - 堅(jiān)定夢(mèng)想,畢生追求!
    posts - 85, comments - 90, trackbacks - 0, articles - 3
      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    Android中多個(gè)Activity間的數(shù)據(jù)共享

    Posted on 2010-01-14 00:18 冰浪 閱讀(3709) 評(píng)論(2)  編輯  收藏 所屬分類(lèi): Android
    在Android中使用Intent在兩個(gè)Activity間傳遞數(shù)據(jù)時(shí),只能是基本類(lèi)型數(shù)據(jù),或者是序列化對(duì)象。Intent是一種基于消息的進(jìn)程內(nèi)和進(jìn)程間通信模型,當(dāng)我們需要在我們應(yīng)用程序內(nèi)部,多個(gè)Activity間進(jìn)行復(fù)雜數(shù)據(jù)對(duì)象共享交互時(shí),使用Intent就顯得很不方便。此時(shí),我們就需要一種數(shù)據(jù)共享的機(jī)制來(lái)實(shí)現(xiàn)。當(dāng)然,直接使用java語(yǔ)言中的靜態(tài)變量是可以的,但在Android中有更為優(yōu)雅的實(shí)現(xiàn)方式。

    【轉(zhuǎn)自:http://www.javaeye.com/topic/552758
    【原文鏈接:http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables】

    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的子類(lèi),然后在manifest中申明一下這個(gè)類(lèi),這是android就為此建立一個(gè)全局可用的實(shí)例,你可以在其他任何地方使用Context.getApplicationContext()方法獲取這個(gè)實(shí)例,進(jìn)而獲取其中的狀態(tài)(變量)。

    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. }  

    評(píng)論

    # re: Android中多個(gè)Activity間的數(shù)據(jù)共享  回復(fù)  更多評(píng)論   

    2010-09-02 10:02 by xhl
    請(qǐng)問(wèn)閣下:在AndroidManifest.xml中如何配置MyApp??

    # re: Android中多個(gè)Activity間的數(shù)據(jù)共享  回復(fù)  更多評(píng)論   

    2010-09-15 10:02 by HKMan
     在AndroidManifest.xml的application加個(gè)name屬性就可以了,如下面所示:

      android:name=".MyApp" android:icon="@drawable/icon"
      android:label="@string/app_name">
    主站蜘蛛池模板: 免费av片在线观看网站| 亚洲综合图色40p| 日本免费人成视频在线观看| 久久亚洲精品无码网站| 久久久无码精品亚洲日韩按摩| 免费jjzz在线播放国产| 中文字幕av无码无卡免费| 免费视频精品一区二区三区| 四虎影视久久久免费| 亚洲AV女人18毛片水真多| 亚洲国产午夜精品理论片 | 亚洲精品中文字幕乱码影院| 久久亚洲国产成人精品无码区| 热99re久久精品精品免费| 久草免费在线观看视频| 三年片在线观看免费观看大全动漫| 午夜成人无码福利免费视频| 亚洲AV无码一区二区乱子仑| 亚洲日本中文字幕天天更新| 亚洲卡一卡2卡三卡4麻豆| 亚洲黄色网址大全| 亚洲一区二区中文| 久久亚洲精品无码| 亚洲AV无码专区在线播放中文 | 香蕉视频免费在线| 亚洲成在人线在线播放无码 | 美女被免费喷白浆视频| 青青草无码免费一二三区| 三年片在线观看免费西瓜视频| 一级大黄美女免费播放| a级毛片免费观看网站| GOGOGO免费观看国语| 国产区在线免费观看| 亚洲阿v天堂在线2017免费| 久久成人18免费网站| 国产裸体美女永久免费无遮挡| 久久国产精品免费一区| 香蕉视频在线免费看| 成人性生交大片免费看中文| 麻豆精品成人免费国产片| 久久午夜夜伦鲁鲁片免费无码|