본문 바로가기
안드로이드 자바

[Java][Android] Bundle을 활용해 액티비티 간 데이터 전달하기

by teamnova 2025. 1. 5.
728x90

안녕하세요.

오늘은 Bundle 을 사용해서 액티비티 간 데이터를 전달하는 방법을 예제와 함께 설명하겠습니다.

 

Bundle은 데이터를 key-value 쌍으로 저장하고 전달할 수 있는 객체입니다.

Intent 를 사용해 액티비티를 전환할 때, Bundle 을 이용해서 데이터를 함께 전달할 수 있습니다.

 

전체 코드입니다.

 

MainActivity.java

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText usernameInput = findViewById(R.id.usernameInput);
    EditText ageInput = findViewById(R.id.ageInput);
    EditText nicknameInput = findViewById(R.id.nicknameInput);
    Button sendDataButton = findViewById(R.id.sendDataButton);

    sendDataButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("username", usernameInput.getText().toString());
        bundle.putInt("age", Integer.parseInt(ageInput.getText().toString()));
        bundle.putString("nickname", nicknameInput.getText().toString());
        intent.putExtras(bundle);
        startActivity(intent);
      }
    });

  }
}

 

 

SecondActivity.java

public class SecondActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    TextView resultTextView = findViewById(R.id.resultTextView);

    // 데이터 수신
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
      String username = bundle.getString("username");
      int age = bundle.getInt("age", 0); // 기본값 0
      String nickname = bundle.getString("nickname");
      resultTextView.setText("이름: " + username + "\n나이: " + age + "\n닉네임: " + nickname);
    }
  }
}

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="50dp"
  android:gravity="center">

  <EditText
    android:id="@+id/usernameInput"
    android:hint="이름"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

  <EditText
    android:id="@+id/ageInput"
    android:hint="나이"
    android:inputType="number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

  <EditText
    android:id="@+id/nicknameInput"
    android:hint="닉네임"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

  <Button
    android:id="@+id/sendDataButton"
    android:text="Send Data"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>

 

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center">

  <TextView
    android:id="@+id/resultTextView"
    android:textSize="18sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>

 

사용자가 입력한 데이터를 Bundle에 추가하여 Intent에 담아 SecondActivity를 호출합니다.

SecondActivity 에서는 , getIntent() 를 사용해서 Bundle에 담겨있는 데이터를 추출하고 화면에 표시합니다.

 

시연 영상입니다.