android Acitivity之間的幾種傳值方式(^_^)

來源:互聯網
上載者:User

標籤:

 對於開發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之間的幾種傳值方式(^_^)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.