Android基礎】頁面跳轉與傳值(Activity跳轉與傳值)

來源:互聯網
上載者:User

一個Android應用程式很少會只有一個Activity對象,如何在多個Activity之間進行跳轉,而且能夠互相傳值是一個很基本的要求。

本次我們就講一下,Android中頁面跳轉以及傳值的幾種方式!

Activity跳轉與傳值,主要是通過Intent類來串連多個Activity,通過Bundle類來傳遞資料。

最常見最一般的頁面跳轉代碼,很簡單,如下:

view plaincopy to clipboardprint?
Intent intent = new Intent(A.this, B.class); 
startActivity(intent); 
 

也可以這樣寫:

view plaincopy to clipboardprint?
Intent intent = new Intent(); 
intent.setClass(A.this, B.class); 
startActivity(intent); 
 

只要這兩句,就可以實現從A頁面跳轉到B頁面了。  (A、B均繼承自Activity)

有的時候,在跳轉頁面時還需要傳遞資料,這個時候如何做呢?

如果資料比較少,比如只要傳一個名字,那麼只要j加一句"intent.putExtra("Name", "feng88724");"即可,代碼如下:

view plaincopy to clipboardprint?
Intent intent = new Intent(); 
intent.setClass(A.this, B.class); 
intent.putExtra("Name", "feng88724"); 
startActivity(intent); 
 

如果資料比較多,就需要使用 Bundle類了,代碼如下: (說明直接看注釋)

view plaincopy to clipboardprint?
Intent intent = new Intent(A.this, B.class); 
 
/* 通過BundleObject Storage Service需要傳遞的資料 */ 
Bundle bundle = new Bundle(); 
/*字元、字串、布爾、位元組數組、浮點數等等,都可以傳*/ 
bundle.putString("Name", "feng88724"); 
bundle.putBoolean("Ismale", true); 
 
/*把bundle對象assign給Intent*/ 
intent.putExtras(bundle); 
 
startActivity(intent); 
 

以上我們講的都是如何進行頁面跳轉及資料傳遞,那麼在另一個頁面B上,應該如何接收資料呢?

在A頁面上是以Bundle封裝了對象,自然在B頁面也是以Bundle的方式來解開封裝的資料。主要通過"getIntent().getExtras()"方法來擷取Bundle,然後再從Bundle中擷取資料。 也可以通過" this.getIntent().getStringExtra("Name");"方法直接從Intent中擷取資料。

從Bundle擷取資料的代碼:

view plaincopy to clipboardprint?
@Override 
 public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        /*載入頁面*/ 
        setContentView(R.layout.main); 
         
        /*擷取Intent中的Bundle對象*/ 
        Bundle bundle = this.getIntent().getExtras(); 
         
        /*擷取Bundle中的資料,注意類型和key*/ 
        String name = bundle.getString("Name"); 
        boolean ismale = bundle.getBoolean("Ismale"); 
         

 

有時,在頁面跳轉之後,需要返回到之前的頁面,同時要保留使用者之前輸入的資訊,這個時候該怎麼辦呢?

在頁面跳轉後,前一個Activity已經被destroy了。如果要返回並顯示資料,就必須將前一個Activity再次喚醒,同時調用某個方法來擷取並顯示資料。

要實現這個效果,需要做以下幾步:

1. 首先,從A頁面跳轉到B頁面時,不可以使用"startActivity()"方法,而要使用"startActivityForResult"方法。

2. 在A頁面的Activity中,需要重寫"onActivityResult"方法

 
view plaincopy to clipboardprint?
@Override 
protected void onActivityResult(int requestCode,int resultCode,Intent data){ 
     
    switch(requestCode){ 
    case RESULT_OK: 
        /*取得來自B頁面的資料,並顯示到畫面*/ 
        Bundle bundle = data.getExtras(); 
         
        /*擷取Bundle中的資料,注意類型和key*/ 
        String name = bundle.getString("Name"); 
        boolean ismale = bundle.getBoolean("Ismale"); 
    } 

 

3. 在B頁面上加一個返回按鈕,並在事件寫如下代碼:

 
view plaincopy to clipboardprint?
/*給上一個Activity返回結果*/ 
B.this.setResult(RESULT_OK,intent); 
/*結束本Activity*/ 
B.this.finish(); 
 

本講就這麼多。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.