Android編程中context及全局變量實(shí)例詳解
本文實(shí)例講述了Android編程中context及全局變量的用法。分享給大家供大家參考,具體如下:
今天在研究context的時(shí)候,對(duì)application和activity context有了一定的了解,下面是從網(wǎng)上復(fù)制過(guò)來(lái)的資料
Application context和Activity context的區(qū)別:
這是兩種不同的context,也是最常見的兩種。第一種中context的生命周期與Application的生命周期相關(guān)的,context隨著Application的銷毀而銷毀,伴隨application的一生,與activity的生命周期無(wú)關(guān)。第二種中的context跟Activity的生命周期是相關(guān)的,但是對(duì)一個(gè)Application來(lái)說(shuō),Activity可以銷毀幾次,那么屬于Activity的context就會(huì)銷毀多次。至于用哪種context,得看應(yīng)用場(chǎng)景,個(gè)人感覺用Activity的context好一點(diǎn),不過(guò)也有的時(shí)候必須使用Application的context。application context可以通過(guò)Context.getApplicationContext或者Activity.getApplication方法獲取。
還有就是,在使用context的時(shí)候,小心內(nèi)存泄露,防止內(nèi)存泄露,注意一下幾個(gè)方面:
1. 不要讓生命周期長(zhǎng)的對(duì)象引用activity context,即保證引用activity的對(duì)象要與activity本身生命周期是一樣的
2. 對(duì)于生命周期長(zhǎng)的對(duì)象,可以使用application context
3. 避免非靜態(tài)的內(nèi)部類,盡量使用靜態(tài)類,避免生命周期問題,注意內(nèi)部類對(duì)外部對(duì)象引用導(dǎo)致的生命周期變化
現(xiàn)在回到正題,說(shuō)一下android全局變量,在平時(shí)的開發(fā)過(guò)程中,有時(shí)候可能會(huì)需要一些全局?jǐn)?shù)據(jù),來(lái)讓應(yīng)用中的所有Activity和View都能訪問到,大家在遇到這種情況時(shí),可能首先會(huì)想到自己定義一個(gè)類,然后創(chuàng)建很多靜態(tài)成員,android已經(jīng)為我們提供了這種情況的解決方案:
在Android中,有一個(gè)Application類,在Activity中可以使用getApplication()方法獲得實(shí)例,使用它就可以獲得當(dāng)前應(yīng)用的主題、資源文件中的內(nèi)容等,這個(gè)類更靈活的一個(gè)特性就是可以被繼承,來(lái)添加自己的全局屬性。例如開發(fā)一個(gè)游戲,需要保存分?jǐn)?shù),那么我們就可以繼承Application,下面是個(gè)demo供大家參考
首先,先寫個(gè)Application的子類:
import android.app.Application;
public class GameApplication extends Application {
private int score;
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
然后在manifest.xml文件里面修改:
<application android:name=".GameApplication" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".DemoActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ResultActivity"></activity>
</application>
注意到添加了android:name=".GameApplication" 。
修改完了以后,再往下看:
public class DemoActivity extends Activity {
public Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
((GameApplication)getApplication()).setScore(100);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(DemoActivity.this, ResultActivity.class);
startActivity(intent);
}
});
}
}
在這個(gè)activity里面設(shè)置了分?jǐn)?shù),我們可以在別的activity里面取出來(lái):
public class ResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
TextView tv=(TextView)findViewById(R.id.tv);
int score=((GameApplication)getApplicationContext()).getScore();
tv.setText("你的成績(jī)是:"+score);
}
}
這只是個(gè)簡(jiǎn)單的例子,當(dāng)然,想要完成以上功能,使用intent傳值就可以了,這樣還顯得麻煩,但是,如果有很多activity,使用這種方法就會(huì)發(fā)現(xiàn)很有用,是不是使用sharepreference也可以完成類似功能呢,可以,但是,效率方面就要比這個(gè)差很多了,sharepreference主要是用來(lái)存儲(chǔ)數(shù)據(jù),你可以退出程序時(shí)把所需要保存的簡(jiǎn)單數(shù)據(jù)保存到sharepreference里面,當(dāng)然復(fù)雜的數(shù)據(jù),還得使用sqllite。
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android 中Context的使用方法詳解
- Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對(duì)象的方法詳解
- Android全局獲取Context實(shí)例詳解
- Android編程實(shí)現(xiàn)為L(zhǎng)istView創(chuàng)建上下文菜單(ContextMenu)的方法
- Android context源碼詳解及深入分析
- Android面試筆記之常問的Context
- 談?wù)凙ndroid里的Context的使用實(shí)例
- 避免 Android中Context引起的內(nèi)存泄露
- 安卓Android Context類實(shí)例詳解
- 詳解Android中的Context抽象類
- 深入解析Android App開發(fā)中Context的用法
- Android編程獲取全局Context的方法
- Android中ContextMenu用法實(shí)例
- android基礎(chǔ)教程之context使用詳解
- Android獲取其他包的Context實(shí)例代碼
- android中Context深入詳解
相關(guān)文章
Android字體相關(guān)知識(shí)總結(jié)
最近接到一個(gè)需求,大致內(nèi)容是:全局替換當(dāng)前項(xiàng)目中的默認(rèn)字體,并引入 UI 設(shè)計(jì)師提供的一些新字體。于是對(duì)字體做了些研究,把自己的一些心得分享給大家。注意:本文所展示的系統(tǒng)源碼都是基于Android-30 ,并提取核心部分進(jìn)行分析2021-06-06
Android textview 實(shí)現(xiàn)長(zhǎng)按自由選擇復(fù)制功能的方法
下面小編就為大家?guī)?lái)一篇Android textview 實(shí)現(xiàn)長(zhǎng)按自由選擇復(fù)制功能的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
android開發(fā)仿ios的UIScrollView實(shí)例代碼
下面小編就為大家分享一篇android開發(fā)仿ios的UIScrollView實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android ViewFlipper的詳解及實(shí)例
這篇文章主要介紹了Android ViewFlipper的詳解及實(shí)例的相關(guān)資料,通過(guò)本文希望能幫助大家理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08
Android自定義view之太極圖的實(shí)現(xiàn)教程
這篇文章主要給大家介紹了關(guān)于Android自定義view之太極圖的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

