컴퓨터/이론: 안드로이드
[2017.09.20] 10. Activity 사이 데이터 통신
heepie
2017. 9. 20. 09:18
개념
안드로이드에서 Activity 사이에 데이터를 전송하기 위해서는 intent라는 객체를 사용한다. 아래 그림과 같이 intent를 통해 데이터를 전송한다.

통신하는 방법은
전송 방법
1) 인텐트에 직접 전달
2) Bundle을 통해 전달
추출 방법
1) 인텐트로 직접 추출
2) Bundle을 통해 추출
그림과 같이 총 4가지의 경우의 수가 나올 수 있다.

실습
인텐트 전달 - 인텐트 추출
Host Activity |
Client Activity |
| Intent intent = new Intent(this, subActivity.class); intent.putExtra("key", "heepie"); startActivity(intent); | cs |
|
| Intent intent = getIntent(); str = intent.getStringExtra("key"); // 테스트를 위한 출력 Toast.makeText(this, str, Toast.LENGTH_LONG).show(); | cs |
|
인텐트 전달 - Bundle 추출
Host Activity |
Client Activity |
| Intent intent = new Intent(this, subActivity.class); intent.putExtra("key", "heepie"); startActivity(intent); | cs |
|
| Intent intent = getIntent(); Bundle bundle = intent.getExtras(); // bundle에서 'key'를 통해 'value' 추출 str = (String)bundle.get("key"); // 테스트를 위한 출력 Toast.makeText(this, str, Toast.LENGTH_LONG).show(); | cs |
|
Bundle 전달 - 인텐트 추출
Host Activity |
Client Activity |
| Intent intent = new Intent(this, subActivity.class); Bundle bundle = new Bundle(); bundle.putString("key", "hello Bundle"); intent.putExtras(bundle); startActivity(intent); | cs |
|
| Intent intent = getIntent(); str = intent.getStringExtra("key"); // 테스트를 위한 출력 Toast.makeText(this, str, Toast.LENGTH_LONG).show(); | cs |
|
Bundle 전달 - Bundle 추출
Host Activity |
Client Activity |
| Intent intent = new Intent(this, subActivity.class); Bundle bundle = new Bundle(); bundle.putString("key", "hello Bundle"); intent.putExtras(bundle); startActivity(intent); | cs |
|
| Intent intent = getIntent(); Bundle bundle = intent.getExtras(); // bundle에서 'key'를 통해 'value' 추출 str = (String)bundle.get("key"); // 테스트를 위한 출력 Toast.makeText(this, str, Toast.LENGTH_LONG).show(); | cs |
|
# 액티비티 통신 #액티비티간 통신 #액티비티 데이터 #액티비티 데이터 전송