Android > Activity ȸ鰣 À̵¿½Ã µ¥ÀÌÅÍ Àü´Þ (startActivityForResult)
µî·ÏÀÏ : 2017-07-05 11:12
Á¶È¸¼ö : 53,372
¿ì¼± activity1 ¾×ƼºñƼ¿Í activity2 ¾×ƼºñƼ°¡ ÀÖ´Ù°í °¡Á¤ÇÑ´Ù.
µ¿ÀÛÀº activity1 ¿¡¼ activity2·Î À̵¿ÇÑ ÈÄ ´Ù½Ã activity2¿¡¼ activity1·Î À̵¿ÇÏ¸ç µ¥ÀÌÅ͸¦ ³Ñ±â´Â °ÍÀÌ´Ù.
1. intent¸¦ »ç¿ëÇØ activity2 ¾×ƼºñƼ·Î ³Ñ¾î°¨
public static final int REQUEST_CODE = 1001;
......
Intent intent = new Intent(getBaseContext(), activity2.class); // activity2·Î À̵¿ÇÏ´Â intent »ý¼º
//intent Á¤º¸¸¦ startActivityForResult¿¡ ½Ç¾î º¸³¿(ù¹ø° ÆĶó¹ÌÅÍ´Â intent, µÎ¹ø° , µ¥ÀÌÅ͸¦ ±¸ºÐÇÒ ±¸ºÐÀÚ°ª)
startActivityForResult(intent,REQUEST_CODE);
2. activity2 ¾×ƼºñƼ¿¡¼ ÀÛ¾÷À» ³¡³½ ÈÄ activity1 ¾×ƼºñƼ¿¡ °ªÀ» Àü´ÞÇÏ°í finish() ½ÃÅ´
Intent myIntent1 = new Intent();
myIntent1.putExtra("name", "mike");
setResult(1, myIntent1);
finish();
name:mike °ªÀ» activity1 À¸·Î ³Ñ±è
activity1 ¿¡¼ (onActivityResult) resultCode °ª 1À» ¹ÞÀ½.
3. activity1 ¾×ƼºñƼ¿¡¼ activity2 ¾×ƼºñƼ¿¡¼ º¸³½ °ªÀ» ¹Þ¾Æ »ç¿ëÇϱâ À§ÇØ onActivityResult ¸Þ¼µå ±¸Çö
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(getBaseContext(), "request code : "+requestCode,Toast.LENGTH_SHORT).show();
if(requestCode == REQUEST_CODE)
{
Toast.makeText(getBaseContext(), "On ActivityResult called with code : "+resultCode , Toast.LENGTH_LONG).show();
}
if(resultCode ==1)
{
String name = data.getStringExtra("name");
Toast.makeText(getBaseContext(), "result name : "+name,Toast.LENGTH_LONG).show();
}
}