2009-07-02 4 views
11

주 활동에는 설정된 값을 가진 일부 변수가 포함됩니다. 기본 액티비티의 데이터로 채워야하는 양식을 사용하여 하위 액티비티를 만들었으므로 데이터가 시작될 때 하위 액티비티로 전달되어야합니다.Android : 하위 활동으로 데이터를 전달하는 방법?

주 활동의 하위 활동에 변수 값을 전달하는 방법을 아는 사람이 있습니까?

감사합니다.

답변

20

당신은 보통에서 onCreate 이벤트

int value = getIntent().getExtras().getInt("key"); 

나는이 hepls 희망에,이 방법으로 값을 얻을 하위 활동에 다음의 주요 활동에

Intent i = new Intent(this, YourMainClass.class); 
i.putExtra("key", value); 

끝이 방법을 사용할 수 있습니다.

2

주요 활동에서 작동합니까?

Intent i = new Intent(this, YourMainClass.class); 
i.putExtra("key", value); 

은 얹는 :

String value = getIntent().getExtras().getString("key"); 

그리고 당신은 다음과 같은 여러 "기타"를 추가하거나 뭔가 할 수 있습니까?

i.putExtra("key", value1); 
i.putExtra("key2", value2); 
i.putExtra("key3", value3); 

감사합니다 ...

0

가 작동이 시도 :

activity1.class :

Intent i = new Intent(activity1.this,activity2.class); 

Bundle b = new Bundle(); 
b.putString("name", "your value need to pass here"); 

i.putExtras(b); 
startActivity(i); 

activity2.class :

Bundle b = this.getIntent().getExtras(); 

String name = b.getString("name"); 

((TextView)findViewById(R.id.textView1)).setText(name); 
관련 문제