2swan

RegisterForActivityResult 본문

Programming/Android Function

RegisterForActivityResult

2swan 2023. 8. 12. 19:17

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

        <TextView
            android:id="@+id/tv_result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:hint="텍스트"
            android:textSize="25sp"/>

        <Button
            android:id="@+id/btn_move"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="이동"
            android:textSize="25sp"/>
        
</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="wrap_content"
    android:orientation="vertical"
    tools:context=".SubActivity">


    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Text"
        android:textSize="25sp"/>

    <Button
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="버튼"
        android:layout_gravity="center"
        android:textSize="25sp"/>


</LinearLayout>

main.activity

public class MainActivity extends AppCompatActivity {
    TextView tv_result;

    Button btn_move;

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

        tv_result  = (TextView) findViewById(R.id.tv_result);
        btn_move = (Button) findViewById(R.id.btn_move);

        btn_move.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    Intent intent =  new Intent(MainActivity.this, SubActivity.class);
                    getSubActivitiyResult.launch(intent);  //startActivityResult 랑 동일한 기능 수행
            }
        });

    }
    private  final ActivityResultLauncher<Intent>getSubActivitiyResult = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                //서브 액티비티로부터 돌아올 때의 서브 액티비티에서 벌어지는 어떠한 결과 값을 받아 올 수 있는 통로
                if(result.getResultCode() == RESULT_OK ){
                    // 서브 액티비티의 입력 값을 메인에서 받아서 텍스트뷰에 표시..
                    tv_result.setText(result.getData().getStringExtra("value"));
                }
            }
    );

}

 

sub.activity

public class SubActivity extends AppCompatActivity {

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

        EditText et_input = (EditText) findViewById(R.id.et_input);

        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String strInputValue = et_input.getText().toString();

                //메인 액티비로 다시 돌아갈 때 입력필드의 입력 값을 되돌려 준다
                Intent intent = new Intent();
                intent.putExtra("value", strInputValue);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}

결과 값

'Programming > Android Function' 카테고리의 다른 글

Navigation Menu 커스텀  (0) 2023.08.12
ListView  (0) 2023.08.12
ImageView & Toast  (0) 2023.08.12
Intent 화면전환  (0) 2023.08.12
SQlite 예제  (0) 2023.08.08