언어/ANDROID
[Android] Intent 값 전달
Sime
2016. 9. 1. 14:39
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”); |
반응형