Activity간 데이터전달(putExtra, getExtras,getIntent)

Posted by ITPangPang
2016.04.23 01:07 안드로이드(android)/인텐트


Activity간 데이터전달

(putExtra, getExtras, getIntent)



- 이번글에서는 Intent를 통해서 Activity를 전환후에 그 전 Activity에서 사용하던 값들을 어떻게 가져오는지 살펴본다.


- 엄청 많이 사용되는데 엄청 쉬우니까 슬쩍 봐두면 좋다.




어려운 부분은 없으니

바로 살펴보면



[String]


먼저 Main에서 

Second Activity로

전환하면서

putExtra로

String값을 보낸다

public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String str = "hi";

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("itpangpang",str);
startActivity(intent);
}
}


SecondActivity에서

값을 받는다.

public class SecondActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
Log.d("ITPANGPANG",intent.getExtras().getString("itpangpang"));
}
}





새로운 코드가

3줄 쓰였다


MainActivity

intent.putExtra("itpangpang",str);

Key, Value를 통해서 값을 넣는다


itpangpang이라는 보관함에

str(hi)를 보관하는 것이다.


SecondActivity

getIntent();


말 그대로 

자신을 호출한

Activity한테서

인텐트값을

받는다는 말입니다.


intent.getExtras().getString("itpangpang");

itpangpang 보관함에 들어있는

String을 가져오겠다는 뜻입니다.



[int]

public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

int i = 1;
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("itpangpang", i);
startActivity(intent);
}
}


public class SecondActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
Log.d("ITPANGPANG",""+intent.getExtras().getInt("itpangpang"));
}
}



[String 배열]

public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String strArray[] = {"hi","hello"};

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("itpangpang", strArray);
startActivity(intent);
}
}



public class SecondActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
int i = -1;
String str[] = intent.getExtras().getStringArray("itpangpang");
while(true)
{
i++;
try
{
Log.d("ITPANGPANG",""+str[i]);
}
catch (Exception e){break;}
}
}
}







[Boolean]

public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean isboolean = false;

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("itpangpang", isboolean);
startActivity(intent);
}
}



public class SecondActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
Log.d("ITPANGPANG",""+intent.getExtras().getBoolean("itpangpang"));
}
}



[Serializable를 이용해서 한번에 보내기]


[MainActivity.java]

public class MainActivity extends ActionBarActivity 
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

boolean isboolean = false;
String str = "hi";
String strArray[] = {"hi", "hello"};
int i = 1;
int intArray[] = {1, 2, 3, 4};

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Data data = new Data(isboolean, str, strArray,i, intArray);
intent.putExtra("itpangpang", data);
startActivity(intent);
}
}


[Data.java]

public class Data implements Serializable
{
boolean isboolean;
String str;
String strArray[];
int i;
int intArray[];

public Data(boolean isboolean, String str, String[] strArray, int i, int[] intArray)
{
this.isboolean = isboolean;
this.str = str;
this.strArray = strArray;
this.i = i;
this.intArray = intArray;
}
}



[SecondActivity.java]

public class SecondActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
Data data = (Data)intent.getSerializableExtra("itpangpang");
Log.d("ITPANGPANG",""+data.isboolean);
Log.d("ITPANGPANG",""+data.str);
Log.d("ITPANGPANG",""+data.strArray.length);
Log.d("ITPANGPANG",""+data.i);
Log.d("ITPANGPANG",""+data.intArray.length);
}
}






이런식으로 한번에 가져올 수 있다.

배열들은 귀찮아서 Length로 받았다.

저작자 표시 비영리 변경 금지
신고
이 댓글을 비밀 댓글로