2013-04-12 34 views
1

我想從一個活動以下三個元素傳遞給另一個:傳遞3個元素(意圖)

在主活性(MainActivity:

String a = "a"; 
String b = "b"; 
String c = "c"; 

我曾嘗試沒有成功以下):

Bundle extras = new Bundle(); 
extras.putString("a", a); 
extras.putString("b", b); 
extras.putString("c", c); 
Intent intent = new Intent(MainActivity.this, SubActivity.class); 
intent.putExtras(extras); 
startActivity(intent); 

在子活性(子活動):

Bundle extras = new Bundle(); 
String a = extras.getString("a"); 
String b = extras.getString("b"); 
String c = extras.getString("c"); 

回答

0

在你的子活動

相反

Bundle extras = new Bundle(); 

使用下面的

Bundle extras = getIntent().getExtras(); 
if(extras!=null) 
{ 
     String a = extras.getString("a"); 
     String b = extras.getString("b"); 
     String c = extras.getString("c"); 
} 
0

在SubActivity中,您應該通過調用getIntent().getExtras();來獲得Bundle,而不是創建一個新的Bundle。

public class SubActivity extends Activity { 
    public void onCreate(Bundle saved) { 
     super.onCreate(saved); 
     setContentView(...); 

     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      // call extras.getString() here 
     } 
    } 
} 
0
String array[] = {"a","b","c"}; 

Intent i = new Intent(A.this, B.class); 
i.putExtra("array", array); 
startActivity(i); 

在活動B:

Bundle extras = getIntent().getExtras(); 
String[] arrayB = extras.getStringArray("array");