자기 개발과 IT 프로그래밍을 위한 여행

물처럼 흐르는 시간, 그 속에서의 여행

안드로이드 기반 앱 개발부/자바 코드 개발청

안드로이드 번들(Bundle)

창조의 새싹 2021. 3. 31. 23:18

액티비티 간 데이터 송수신 예제(추후 보완 수정 작업 예정)

간단 설명

 - 두개의 액티비티 사이에 번들을 통해 데이터 송수신 예제

 - 본 페이지에선 Bundle 내에 문자열, 정수, 문자열 배열, 정수 배열 사용

 

메니페스트 엑티비티 추가

        <activity android:name=".SubActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

 

MainAcitivity.class

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

public class MainActivity extends Activity {

    Button btn;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Bundle bundle = new Bundle();
        bundle.putString("name", "coffee");
        bundle.putInt("num", 1);
        ArrayList<Integer> array = new ArrayList<>();
        array.add(1);
        array.add(2);
        array.add(3);
        bundle.putIntegerArrayList("arrayData",array);
        ArrayList<String> array2 = new ArrayList<>();
        array2.add("A");
        array2.add("B");
        array2.add("C");
        bundle.putStringArrayList("arrayData2",array2);

        btn = findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), SubActivity.class);
                intent.putExtra("myBundle", bundle); startActivity(intent);
            }
        });
    }
}

 

SubActivity.class

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SubActivity extends Activity {

    TextView tv;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("myBundle");

        tv = findViewById(R.id.textView);
        tv.setText("배열 사이즈 : " + bundle.getIntegerArrayList("arrayData").size()+"" + "\n"
                + "1번방 배열 데이터 : " + bundle.getIntegerArrayList("arrayData").get(1) + "\n"
                + "name : " + bundle.getString("name") + ", num : " + bundle.getInt("num") +"\n"
                + "문자열 배열 : " + bundle.getStringArrayList("arrayData2").get(2)
        );

    }
}

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="163dp"
        android:layout_marginLeft="163dp"
        android:layout_marginTop="43dp"
        android:layout_marginEnd="155dp"
        android:layout_marginRight="155dp"
        android:layout_marginBottom="264dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

activity_sub.xml

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>