android不同activity之間共享數(shù)據(jù)解決方法
Android中在不同Activity中傳遞變量,通常使用Intent中Bundle添加變量的操作方法。
保存參數(shù)時(shí):
Intent intent = new Intent();
intent.setClass(A.this, B.class);
Bundle bundle = new Bundle();
bundle.putString("name", "xiaozhu");
intent.putExtras(bundle);
startActivity(intent);
讀取參數(shù):
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
[java] view plaincopy
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
不過(guò)在多個(gè)Activity中經(jīng)常使用同一變量時(shí),使用Bundle則比較麻煩,每次調(diào)用Activity都需要設(shè)置一次。
如想在整個(gè)應(yīng)用中使用,在java中一般是使用靜態(tài)變量,而在android中有個(gè)更優(yōu)雅的方式是使用Application context。
新建一個(gè)類,繼承自Application
class MyApp extends Application {
private String myState;
public String getState() {
return myState;
}
public void setState(String s) {
myState = s;
}
}
在AndroidManifest.xml的application加個(gè)name屬性就可以了,如下面所示:
<application android:name=".MyApp" android:icon="@drawable/icon" android:label="@string/app_name">
使用時(shí):
class Blah extends Activity {
@Override
public void onCreate(Bundle b){
...
MyApp appState = ((MyApp)getApplicationContext());
String state = appState.getState();
...
}
}
- Android通過(guò)ViewModel保存數(shù)據(jù)實(shí)現(xiàn)多頁(yè)面的數(shù)據(jù)共享功能
- Android開(kāi)發(fā)之5.0activity跳轉(zhuǎn)時(shí)共享元素的使用方法
- Android數(shù)據(jù)共享 sharedPreferences 的使用方法
- android與asp.net服務(wù)端共享session的方法詳解
- 詳解Android(共享元素)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)開(kāi)發(fā)實(shí)踐
- Android 仿摩拜單車共享單車進(jìn)度條實(shí)現(xiàn)StepView效果
- Android開(kāi)發(fā)中多進(jìn)程共享數(shù)據(jù)簡(jiǎn)析
- Android設(shè)備間實(shí)現(xiàn)藍(lán)牙(Bluetooth)共享上網(wǎng)
- Android實(shí)現(xiàn)不同apk間共享數(shù)據(jù)的方法(2種方法)
- android編程實(shí)現(xiàn)設(shè)置、打開(kāi)wifi熱點(diǎn)共享供他人連接的方法
- Android編程實(shí)現(xiàn)兩個(gè)Activity之間共享數(shù)據(jù)及互相訪問(wèn)的方法
- Android 7.0應(yīng)用之間如何共享文件
相關(guān)文章
Android自定義動(dòng)態(tài)壁紙開(kāi)發(fā)(時(shí)鐘)
今天小編就為大家分享一篇關(guān)于Android自定義動(dòng)態(tài)壁紙開(kāi)發(fā)(時(shí)鐘),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01解析android中的幫助、about、關(guān)于作者、HELP等提示頁(yè)面
本篇文章是對(duì)android中的幫助、about、關(guān)于作者、HELP等提示頁(yè)面進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android編程開(kāi)發(fā)之打開(kāi)文件的Intent及使用方法
這篇文章主要介紹了Android編程開(kāi)發(fā)之打開(kāi)文件的Intent及使用方法,已實(shí)例形式分析了Android打開(kāi)文件Intent的相關(guān)布局及功能實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android實(shí)現(xiàn)新浪微博一鍵分享的實(shí)例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)新浪微博一鍵分享的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android?studio實(shí)現(xiàn)日期?、時(shí)間選擇器與進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android?studio實(shí)現(xiàn)日期、時(shí)間選擇器與進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Android編程實(shí)現(xiàn)自定義系統(tǒng)菜單背景的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義系統(tǒng)菜單背景的方法,涉及Android菜單menu的實(shí)現(xiàn)及背景圖片的相關(guān)操作技巧,需要的朋友可以參考下2016-01-01Android入門之Style與Theme用法實(shí)例解析
這篇文章主要介紹了Android入門之Style與Theme用法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08Retrofit網(wǎng)絡(luò)請(qǐng)求和響應(yīng)處理重點(diǎn)分析講解
這篇文章主要介紹了Retrofit網(wǎng)絡(luò)請(qǐng)求和響應(yīng)處理重點(diǎn)分析,在使用?Retrofit發(fā)起網(wǎng)絡(luò)請(qǐng)求時(shí),我們可以通過(guò)定義一個(gè)接口并使用Retrofit的注解來(lái)描述這個(gè)接口中的請(qǐng)求,Retrofit會(huì)自動(dòng)生成一個(gè)實(shí)現(xiàn)該接口的代理對(duì)象2023-03-03Android?Flutter控件封裝之視頻進(jìn)度條的實(shí)現(xiàn)
這篇文章主要來(lái)和大家分享一個(gè)很簡(jiǎn)單的控制器封裝案例,包含了基本的播放暫停,全屏和退出全屏,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-06-06