안드로이드-registerForActivityResult?

YAMAMAMO·2021년 12월 18일
0

안드로이드

목록 보기
7/13

resisterForActivity?

startActivityForResult 가 deprecated 되었고 대체재로 registerForActivityResult 사용된다. 사용법을 알아보겠다.

registerForActivityResult 는 ActivityResultContract와 ActivityResultCallback을 가져와서 다른 activity를 실행하는 데 사용할 ActivityResultLauncher를 반환한다.

  • ActivityResultContract는 우리가 결과를 생성하는 데 필요한 입력의 형태와 결과를 출력하는 형태를 정의하고 우리가 intent를 사용하는 작업의 기본적인 계약을 제공한다.

코드

-Main, Sub 액티비티가 있고, Sub 액티비티에서 입력한 데이터를 Main 액티비티에 전달한다.

-MainActivity

public class MainActivity extends AppCompatActivity {
    TextView tv_result;
    Button btn_start;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        tv_result = findViewById (R.id.tv_result);
        btn_start = findViewById (R.id.btn_start);

        btn_start.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getBaseContext (), SubActivity.class);

                launcher.launch(intent);
            }
        });
    }

    ActivityResultLauncher<Intent> launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult> ()
            {
                @Override
                public void onActivityResult(ActivityResult data)
                {
                        Log.d("TAG", "data : " + data);
                    if (data.getResultCode() == Activity.RESULT_OK)
                    {
                        Intent intent = data.getData();
                        String result = intent.getStringExtra ("result");

                        tv_result.setText (result);
                    }
                }
            });
}
<?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/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="결과"
        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_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="start"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.516"
        app:layout_constraintLeft_toLeftOf="@id/tv_result"
        app:layout_constraintRight_toRightOf="@id/tv_result"
        app:layout_constraintTop_toBottomOf="@id/tv_result"
        app:layout_constraintVertical_bias="0.172" />

</androidx.constraintlayout.widget.ConstraintLayout>

SubActivity

public class SubActivity extends AppCompatActivity {
    EditText edt_result;
    Button btn_back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_sub);

         edt_result = findViewById (R.id.edt_result);
         btn_back = findViewById (R.id.btn_back);

         btn_back.setOnClickListener (view -> {
             Intent intent = new Intent ();
             String result = edt_result.getText ().toString ();
             intent.putExtra ("result", result);
             setResult (RESULT_OK, intent);
             finish ();

         });
    }
}
<?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=".SubActivity">

    <EditText
        android:id="@+id/edt_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="반환값을 작성해주세요"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <Button
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="back"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/edt_result"
        app:layout_constraintHorizontal_bias="0.488"
        app:layout_constraintStart_toStartOf="@+id/edt_result"
        app:layout_constraintTop_toBottomOf="@+id/edt_result"
        app:layout_constraintVertical_bias="0.203" />

</androidx.constraintlayout.widget.ConstraintLayout>



Sub 액티비티에서 hi friends를 입력받아 Main 액티비티의 TextView에 띄어진 것을 확인할 수 있다.

profile
안드로이드 개발자

0개의 댓글