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

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

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

    冰浪

    哥已不再年輕 - 堅定夢想,畢生追求!
    posts - 85, comments - 90, trackbacks - 0, articles - 3
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Android中多個Activity間的數據共享

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

    【轉自: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.

    --如想在整個應用中使用,在java中一般是使用靜態變量,而在android中有個更優雅的方式是使用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.
    --每個Activity 都是Context,其包含了其運行時的一些狀態,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):
    --方法是創建一個屬于你自己的android.app.Application的子類,然后在manifest中申明一下這個類,這是android就為此建立一個全局可用的實例,你可以在其他任何地方使用Context.getApplicationContext()方法獲取這個實例,進而獲取其中的狀態(變量)。

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

    評論

    # re: Android中多個Activity間的數據共享  回復  更多評論   

    2010-09-02 10:02 by xhl
    請問閣下:在AndroidManifest.xml中如何配置MyApp??

    # re: Android中多個Activity間的數據共享  回復  更多評論   

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

      android:name=".MyApp" android:icon="@drawable/icon"
      android:label="@string/app_name">
    主站蜘蛛池模板: 国产精品亚洲一区二区三区在线观看 | 亚洲久热无码av中文字幕| 国产精品亚洲精品久久精品| 最近中文字幕mv免费高清视频8| 亚洲成a人片在线观看日本| 国产免费伦精品一区二区三区| 亚洲精品高清一二区久久| 亚洲精品无码久久久久A片苍井空| 97在线线免费观看视频在线观看| 精品亚洲aⅴ在线观看| 免费人妻无码不卡中文字幕系| 久久精品国产亚洲AV电影| 永久在线免费观看| 亚洲国产超清无码专区| 青草草色A免费观看在线| 亚洲熟女精品中文字幕| 日韩激情无码免费毛片| 免费无码国产在线观国内自拍中文字幕 | 可以免费观看一级毛片黄a| 亚洲国产日韩精品| 日韩激情淫片免费看| 日韩少妇内射免费播放| 亚洲理论电影在线观看| 1000部夫妻午夜免费| 亚洲色大情网站www| 亚洲精品视频在线看| 免费黄网站在线看| 亚洲伊人久久大香线焦| 国产免费看插插插视频| 美女巨胸喷奶水视频www免费| 亚洲国产精品久久66| 在线免费观看色片| fc2免费人成在线| 亚洲一区二区三区夜色| 成人免费男女视频网站慢动作| 最好2018中文免费视频| 亚洲AV无码成人精品区在线观看| 久久精品免费全国观看国产| 深夜特黄a级毛片免费播放| 久久精品国产亚洲av麻豆色欲| 日本免费一二区在线电影|