티스토리 뷰

Intent로 다른 Activity(예, AnotherActivity.class)를 실행시킬 때는 다음과 같이 한다.

 

Intent intent = new Intent(getApplicationContext(), AnotherActivity.class);

startActivity(intent);

그런데, 여기에 추가로 AnotherActivity 데이터를 전달하고 싶을 때가 있다.

이때 사용할 수 있는 것이 putExtra() 다.

 

Intent intent = new Intent(getApplicationContext(), AnotherActivity.class);

intent.putExtra(“name”, “rio”);
intent.putExtra(“age”, 10);

startActivity(intent);

 

그리고, AnotherActivity에서는 다음과 같이 getExtras()를 이용해서 데이터를 받을 수 있다.

(바로 데이터를 받을 것이므로 보통은 onCreate() 메서드에 구현 한다.)

 

Intent intent = getIntent();

String name = intent.getExtras().getString(“name”);

int age = intent.getExtras().getInt(“age”);


반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2026/01   »
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 31
글 보관함