일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- 안드로이드 Sunflower 스터디
- Fragment
- 막내의막무가내 rxjava
- 주택가 잠실새내
- 막내의 막무가내 알고리즘
- 부스트코스에이스
- 부스트코스
- 막내의막무가내 안드로이드 에러 해결
- flutter network call
- 막내의막무가내 플러터
- 막내의 막무가내
- 막무가내
- 프래그먼트
- 막내의막무가내 프로그래밍
- 안드로이드 sunflower
- 막내의막무가내 코볼 COBOL
- 막내의막무가내 코틀린
- 막내의막무가내 안드로이드
- 막내의막무가내 코틀린 안드로이드
- 프로그래머스 알고리즘
- 막내의막무가내 SQL
- 막내의막무가내 일상
- 2022년 6월 일상
- 안드로이드
- 막내의막무가내 안드로이드 코틀린
- 막내의막무가내
- 막내의막무가내 목표 및 회고
- 막내의막무가내 플러터 flutter
- 주엽역 생활맥주
- 막내의막무가내 알고리즘
- Today
- Total
막내의 막무가내 프로그래밍 & 일상
[안드로이드] intent.putExtra("data", data) vs intent.putExtras(bundle); 본문
[안드로이드] intent.putExtra("data", data) vs intent.putExtras(bundle);
막무가내막내 2019. 4. 20. 15:11
[2021-04-15 업데이트]
https://stackoverflow.com/questions/11900266/intent-putextrastring-bundle-vs-intent-putextrabundle
Intent.putExtra(String,Bundle) vs Intent.putExtra(Bundle)
This question may sound stupid but I wana know When do we put activity name in Intent.putExtra()? In one case we are putting extra only with bundle and in other case we are passing it with class na...
stackoverflow.com
위 사이트는 bundle을 putExtra로 키 벨류값으로 넣어줄지 , putExtras로 번들만 넣어줄지 하는건데 나의 포스팅 주제와는 다르지만 흥미로워서 넣어봤다.
참고로, 인텐트하고 번들의 차이점을 쉽게말하면 인텐트는 택배를 배달하는 택배트럭이나 택배기사, 번들은 택베에 비유할수있다.
그리고 인텐트 안에 번들이 있다. 즉 실제데이터는 인텐트안의 번들에 넣어지는것이다.
보통 액티비티끼리 데이터를 주고 받을때 intent를 사용하는데 크게 두가지 방법이있다.
예시를 들어봤다. (SEND액티비티에서 Receive액티비티로 데이터전달하는 예시이다.)
<1>인텐트에 put해주는 형식
*SEND
Intent intent = new Intent(context, ReceiveActivity.class);
intent.putExtra("age", 123);
intent.putExtra("name", "Loopy");
Intent intent = getIntent();
startActivityForResult(intent, 102); // 혹은 startActivity(intent);
*RECEIVE
Intent intent = getIntent();
int age = intent.getExtras().getInt("age"); //intent.getIntExtra("age") 라고해도됨
String name = intent.getExtras().getString("name"); //intent.getStringExtra("name") 라고해도됨
setResult(RESULT_OK, intent); //응답 전달 후
finish(); //종료
//Bundle bundle = intent.getExtras();
//int age = bundle.getInt("age");
//이런식으로 받을수도 있다.
참고로 intent.getExtras()가 인텐트안에 있는 Bundle을 의미한다고 보면된다. (인텐트의 Bundle을 반환해주는 함수임)
intent.getIntExtra("id); 는 번들의 해당 아이디의 int형 값을 반환해주는거다.
<2> 번들에 put해줘서 bundle을 인텐트에 put해주는 형식
*SEND
Intent intent = new Intent(context, ReceiveActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("age", 123);
bundle.putString("name", "Loopy");
intent.putExtras(bundle); //bundle로 한번에 넣어줌
// (intent.putExtra("bundle", bundle);
// =>이런식으로 보냈다면 받는쪽에서 BundleData = Intent.getBundleExtra("bundle"); 로 받는다.
startActivityForResult(intent, 102); // 혹은 startActivity(intent);
*RECEIVE
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
int age = bundle.getInt("age");
String name = bundle.getString("name);
setResult(RESULT_OK, intent); //응답 전달 후
finish(); //종료
보통은 putInt(), putStrung() 처럼 put하고 타입까지 써야해서 2번방식이 손이 더많이가긴하지만 bundle에 담아서 번들을 인텐트에 넣어 부가데이터를 보내주는게 좋다고한다. 한번에 보내줘서 더 빠르다고도한다. 밑에 링크에서 두 방식의 함수 내부 구현 방식에 대한 차이를 설명하며 왜 빠른지 말해주고 있다.
2번 방식으로 보내는 습관을 기르자!
그리고 intent에 번들을 넣어줄 때도 intent.putExtra("bundle", bundle) 보다 intent.putExtras(bundle)의 차이는 처음 올려논 사이트에 들어가면 나오지만
=================
이러한 차이가있다고 한다.
난 후자처럼 한번에 putExtras(bundle)이 난거같다.
댓글과 공감은 큰 힘이됩니다.!
'안드로이드 > 자바 & Previous' 카테고리의 다른 글
[안드로이드] 내가 올렸던 질문 답변 - 파이어베이스 데이터베이스 관련 (0) | 2019.04.24 |
---|---|
[안드로이드] 구글맵(GoogleMapAPI) 예제 (주석참고) (5) | 2019.04.23 |
[안드로이드] 기억노트 - 쉐이프 드로어블 (shape drawable) + <layer-list> 기록 (0) | 2019.04.05 |
[안드로이드] 유튜브 썸네일 이미지 가져오는 방법 (3) | 2019.04.03 |
[안드로이드] PhotoView 사진 줌인 줌아웃 (확대 및 축소 드래그) 라이브러리 (8) | 2019.04.02 |