標籤:
對於開發app 來說,資料的傳遞肯定是少不了的啦,其實app 的本質就是用來呈現資料的。
好的
方式一 Intent.putExtra(TAG,DATA);
應用情境 對於傳送單一資料,而又只在兩個Activity之間傳送的的。
發出 sendActivity
Intent intent=new Intent(sendActivity.this,receiveActivity.class);
intent.putExtra(TAG,DATA);
startActivity(intent);
接收 receiveActivity
在receiveActivity 的onCreate() 方法下
String name=getIntent().getIntExtra(TAG);
註:TAG 是一個String 的標誌
DATA 是資料 可以是String ,int,bool等。
方式二 Intent.putExtras(Bundle);
應用情境 對與多個資料 而又只在兩個Activity之間傳送的的。
發出 sendActivity
Intent intent=new Intent(sendActivity.this,receiveActivity.class);
Bundle bundle=new Bundle();
bundle.putString("資料一", 資料一);
bundle.putString("資料二", 資料二);
bundle.putString("資料三", 資料三);
。。。。
intent.putExtras(bundle);
startActivity(intent);
接收 receiveActivity
資料一=this.getIntent().getExtras().getString(" 資料一");
資料二=this.getIntent().getExtras().getString(" 資料二");
資料三=this.getIntent().getExtras().getString(" 資料三");
。。。。
注 Bundle是一個封裝資料的對象 ,不知道你們是怎麼理解,我就是這樣理解的可以封裝很多類型的。
方式 三 Application
應用情境 對與多個資料 或 但單個資料都可以 根據自己需求定義
需要到AndroidManifest.xml 的 applicaion 節點 的android:name 屬性加上 Application類名。
android Acitivity之間的幾種傳值方式(^_^)