2012-03-01 51 views
0

假設我有一項活動獲取一個cookie,然後開始一項新活動。如何將cookie傳遞給新的活動?

Intent myIntent = new Intent(this, act2.class); 
this.startActivity(myIntent); 

但是,如何將該cookie傳遞給新的活動?

謝謝!

回答

2

你必須使用的IntentputExtra方法從一個活動發送數據到另一個活動。

例如。

Bundle bundle=getIntent().getExtras(); 

    if(bundle!=null) 
    { 
     if(bundle.contains("CookieKey")){ 

      String cookie=bundle.getString("CookieKey"); 
     } 
    } 
+0

謝謝,使cookieValue,我只是不MyCookie.toString()? – 2012-03-01 13:23:04

+0

此外,我以後如何再次將該字符串轉換回cookie? :) – 2012-03-01 13:24:50

1

您可以使用Intent.putExtra傳遞間的數據:

Intent myIntent = new Intent(this, act2.class); 
myIntent.putExtra("CookieKey",cookieValue); 
this.startActivity(myIntent); 

,並在其他活動中,你可以使用

String cookie=getIntent().getStringExtra("CookieKey"); 

或以更清晰的方式,你可以用得到這個演員兩項活動。

相關問題