홍드로이드의 야매코딩

#26 안드로이드 스튜디오 다음 화면으로부터 값 전달받기 (StartActivityForResult) 예제 [ 홍드로이드 ] 본문

Android Java

#26 안드로이드 스튜디오 다음 화면으로부터 값 전달받기 (StartActivityForResult) 예제 [ 홍드로이드 ]

홍드로이드 2019. 6. 8. 01:30



< activity_main.xml >

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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=".MainActivity">
 
    <TextView
        android:id="@+id/tv_comeback"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="값을 가져와주세요!"/>
 
    <Button
        android:id="@+id/btn_go"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GO"/>
 
</LinearLayout>
cs




< activity_sub.xml >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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=".SubActivity">
 
    <EditText
        android:id="@+id/et_comeback"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Main으로 보낼 값 입력해주세요."/>
 
    <Button
        android:id="@+id/btn_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="종료"/>
 
 
 
</LinearLayout>
cs






< MainActivity.java >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.example.comebackexample;
 
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    private TextView tv_comeback;
    private Button btn_go;
 
    private static final int REQUEST_CODE = 777// 상수 값을 선언 *상수(항상 같은 수 + 변하지않을 수)
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        tv_comeback = findViewById(R.id.tv_comeback);
        btn_go = findViewById(R.id.btn_go);
 
        btn_go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), SubActivity.class);
                startActivityForResult(intent,REQUEST_CODE);
            }
        });
 
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
 
        if(resultCode == RESULT_OK) {
            Toast.makeText(getApplicationContext(), "수신 성공",Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "수신 실패",Toast.LENGTH_SHORT).show();
        }
 
 
        if(requestCode == REQUEST_CODE) {
            String resultTxt = data.getStringExtra("comeback");
            tv_comeback.setText(resultTxt);
        }
 
    }
}
 
cs








< SubActivity.java >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.example.comebackexample;
 
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
public class SubActivity extends AppCompatActivity {
 
    private EditText et_comeback;
    private Button btn_close;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);
 
        et_comeback = findViewById(R.id.et_comeback);
        btn_close = findViewById(R.id.btn_close);
 
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("comeback", et_comeback.getText().toString()); // 입력폼에 적은 값 담아주기
                setResult(RESULT_OK, intent); // 결과 값 설정
                finish(); // 현재 액티비티 종료
            }
        });
 
 
    }
}
 
cs


Comments