Programming/Android Example

Android 액티비티 데이터 전달 예제 launcher

2swan 2023. 8. 4. 13:49

main.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"
    tools:context=".MainActivity2"
    android:orientation="vertical"
    android:layout_margin="100dp"
    android:layout_gravity="center">

    <Button
        android:id="@+id/btnDataInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="데이터입력"
        android:textSize="30dp"/>
    <Button
        android:id="@+id/btnDataOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="데이터 출력"
        android:textSize="30dp"/>
    <Button
        android:id="@+id/btnDataStu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="학생 정보 입력"
        android:textSize="25dp" />

</LinearLayout>

 

input.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"
    tools:context=".MainActivity2_Input"
    android:layout_gravity="center"
    android:orientation="vertical">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp">

        <TableRow android:layout_margin="5dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="이름"/>

            <EditText
                android:id="@+id/editName"
                android:layout_height="wrap_content"
                android:layout_width="200dp"
                android:hint="이름"/>
        </TableRow>

        <TableRow android:layout_margin="5dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="나이"/>

            <EditText
                android:id="@+id/editAge"
                android:layout_height="wrap_content"
                android:layout_width="200dp"
                android:hint="나이"/>
        </TableRow>

        <TableRow android:layout_margin="5dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="전화번호"/>

            <EditText
                android:id="@+id/editPhone"
                android:layout_height="wrap_content"
                android:layout_width="200dp"
                android:hint="전화번호"/>
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/btnInputCom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="입력완료"
        android:textSize="15sp"/>

</LinearLayout>

 

output.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"
    tools:context=".MainActivity2_Output"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="입력 결과 : "
        android:textSize="25sp"/>
    <TextView
        android:id="@+id/tvDisplay2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="학생 입력 결과 : "
        android:textSize="25sp"/>
    <Button
        android:id="@+id/btnMainBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="돌아가기"
        android:textSize="15sp"
        android:layout_margin="10dp"/>

</LinearLayout>

 

stu.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"
    tools:context=".MainActivity2_Stu"
    android:orientation="vertical"
    android:gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="학번"
            android:textSize="25sp"/>

        <EditText
            android:id="@+id/editSnum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="학번"
            android:layout_weight="1"
            android:textSize="25sp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="이름"
            android:textSize="25sp"/>

        <EditText
            android:id="@+id/editName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="이름"
            android:layout_weight="1"
            android:textSize="25sp"/>

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="전공"
            android:textSize="25sp"/>

        <EditText
            android:id="@+id/editMajor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="전공"
            android:layout_weight="1"
            android:textSize="25sp"/>

    </LinearLayout>

    <Button
        android:id="@+id/btnStuHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="학생입력완료"
        android:textSize="30sp"/>

</LinearLayout>

main.activity

public class MainActivity2 extends AppCompatActivity {
    private String name, age, phone;
    private Student student;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Button btnDataInput = findViewById(R.id.btnDataInput);
        Button btnDataOut = findViewById(R.id.btnDataOut);
        Button btnDataStu = findViewById(R.id.btnDataStu);

        ActivityResultLauncher<Intent> launcher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {

                    @Override
                    public void onActivityResult(ActivityResult result) {
                        Intent data = result.getData();
                        if(result.getResultCode() == Activity.RESULT_OK){

                            name = data.getStringExtra("name");
                           age = data.getStringExtra("age");
                           phone = data.getStringExtra("phone");
                            return;
                        }
                        if(result.getResultCode() == 10){
                           student = (Student) data.getSerializableExtra("student");
                                return;
                        }
                    }
                });


        //입력 ==> return 값 있음  (리턴 값 있으니까 런쳐 호출)
        btnDataInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), MainActivity2_Input.class);

                launcher.launch(intent);
            }
        });

        //출력 ==> return 없음
        btnDataOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), MainActivity2_Output.class  );
                intent.putExtra("name",name);
                intent.putExtra("age",age);
                intent.putExtra("phone",phone);

                intent.putExtra("student",student);

                launcher.launch(intent);
            }
        });

        //학생 정보
        btnDataStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent  = new Intent(getApplicationContext(), MainActivity2_Stu.class);
                intent.putExtra("student",student);
                launcher.launch(intent);
            }
        });

    }
}

 

intput.activity

public class MainActivity2_Input extends AppCompatActivity {

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

        EditText editName = findViewById(R.id.editName);
        EditText editAge = findViewById(R.id.editAge);
        EditText editPhone = findViewById(R.id.editPhone);
        Button btnInputCom = findViewById(R.id.btnInputCom);

        btnInputCom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), MainActivity2.class);
                intent.putExtra("name", editName.getText().toString());
                intent.putExtra("age", editAge.getText().toString());
                intent.putExtra("phone", editPhone.getText().toString());
                setResult(RESULT_OK,intent);
                finish();

            }
        });

    }
}

 

output.activity

public class MainActivity2_Output extends AppCompatActivity {

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

        Button btnMainBack = findViewById(R.id.btnMainBack);
        TextView tvDisplay = findViewById(R.id.tvDisplay);
        TextView tvDisplay2 = findViewById(R.id.tvDisplay2);

        // intent 로  부터 받아오기
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String age = intent.getStringExtra("age");
        String phone = intent.getStringExtra("phone");

        tvDisplay.setText("데이터 : "+name+" // "+age+" // "+phone);
        //Student 객체로 받기
        Student student = (Student) intent.getSerializableExtra("student");
        if(student != null) {
            tvDisplay2.setText("학생 객체 : " + student.getSno() + " : " + student.getName() + " : " + student.getMajor());
        }

        btnMainBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(getApplicationContext(), MainActivity2.class);
                finish();
            }
        });

    }
}

 

stu.activity

public class MainActivity2 extends AppCompatActivity {
    private String name, age, phone;
    private Student student;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Button btnDataInput = findViewById(R.id.btnDataInput);
        Button btnDataOut = findViewById(R.id.btnDataOut);
        Button btnDataStu = findViewById(R.id.btnDataStu);

        ActivityResultLauncher<Intent> launcher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {

                    @Override
                    public void onActivityResult(ActivityResult result) {
                        Intent data = result.getData();
                        if(result.getResultCode() == Activity.RESULT_OK){

                            name = data.getStringExtra("name");
                           age = data.getStringExtra("age");
                           phone = data.getStringExtra("phone");
                            return;
                        }
                        if(result.getResultCode() == 10){
                           student = (Student) data.getSerializableExtra("student");
                                return;
                        }
                    }
                });


        //입력 ==> return 값 있음  (리턴 값 있으니까 런쳐 호출)
        btnDataInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), MainActivity2_Input.class);

                launcher.launch(intent);
            }
        });

        //출력 ==> return 없음
        btnDataOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), MainActivity2_Output.class  );
                intent.putExtra("name",name);
                intent.putExtra("age",age);
                intent.putExtra("phone",phone);

                intent.putExtra("student",student);

                launcher.launch(intent);
            }
        });

        //학생 정보
        btnDataStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent  = new Intent(getApplicationContext(), MainActivity2_Stu.class);
                intent.putExtra("student",student);
                launcher.launch(intent);
            }
        });

    }
}

 

Student.java (class)

public class Student  implements Serializable {

    private  int sno;
    private String name;
    private  String major;
        public Student(){

        }

    public Student(int sno, String name, String major) {
        this.sno = sno;
        this.name = name;
        this.major = major;
    }

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }
}

결과 값