2swan
StartActivityForResult 본문
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=".MainActivity3">
<TextView
android:id="@+id/tv_comeback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:hint="값을 가져와주세요"/>
<Button
android:id="@+id/btn_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO"/>
</LinearLayout>
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"
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>
main.java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
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 (requestCode == REQUEST_CODE) { // requestCode로 비교해야 함
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), "수신 성공", Toast.LENGTH_SHORT).show();
String resultText = data.getStringExtra("value");
tv_comeback.setText(resultText);
} else {
Toast.makeText(getApplicationContext(), "수신 실패", Toast.LENGTH_SHORT).show();
}
}
}
}
sub.java
public class SubActivity extends AppCompatActivity {
private EditText et_comeback;
public 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("value",et_comeback.getText().toString()); //입력폼에 적은 값 담아주기
setResult(RESULT_OK, intent); // 결과 값 설정
finish(); // 현재 액티비티 종료
}
});
}
}
결과 값
'Programming > Android Function' 카테고리의 다른 글
SQlite 예제 (0) | 2023.08.08 |
---|---|
ListView (0) | 2023.08.05 |
Thread & Handler (0) | 2023.08.05 |
Fragment (액티비티 조각 모음) (0) | 2023.08.05 |
SharedPreferences (임시저장) (0) | 2023.08.05 |