How to deal with the starActivityForResult() deprecation

  • I was on the way to following this getting started documents of Android Studio.
  • But I was stucked at this step of the documents because the startActivityForResult()is deprecated.
  • I didn’t want to stop this clone coding at the last step of that course so I started to find a solution for this issue.
  • This StackOverflow answer gives me an solution so I replaced the deprecated code.
    • the old deprecated way ( extracted from this link)
      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
      
          
      public class MainActivity extends AppCompatActivity {
           
          ...
               
          public void launchSecondActivity(View view) {
              Log.d(LOG_TAG, "Button clicked!");
              Intent intent = new Intent(this, SecondActivity.class);
              String message = mMessageEditText.getText().toString();
              intent.putExtra(EXTRA_MESSAGE, message);
              startActivityForResult(intent, TEXT_REQUEST);
          }
               
          @Override
          public void onActivityResult(int requestCode, int resultCode, Intent data) {
              super.onActivityResult(requestCode, resultCode, data);
      
              if (requestCode == TEXT_REQUEST) {
                  if (resultCode == RESULT_OK) {
                      String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY);
                      mReplyHeadTextView.setVisibility(View.VISIBLE);
                      mReplyTextView.setText(reply);
                      mReplyTextView.setVisibility(View.VISIBLE);
                  }
              }
          }
      }
      
    • the new androidx way ( TEXT_REQUEST not used. delete it!)
      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
      
      public class MainActivity extends AppCompatActivity {
        ...
            
        public void launchSecondActivity(View view) {
            Log.d(LOG_TAG, "Button clicked!");
            Intent intent = new Intent(this, SecondActivity.class);
            String message = mMessageEditText.getText().toString();
            intent.putExtra(EXTRA_MESSAGE, message);
            launchSomeActivity.launch(intent);
        }
      
        ActivityResultLauncher<Intent> launchSomeActivity = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        String reply = result.getData().getStringExtra(SecondActivity.EXTRA_REPLY);
                        mReplyHeadTextView.setVisibility(View.VISIBLE);
                        mReplyTextView.setText(reply);
                        mReplyTextView.setVisibility(View.VISIBLE);
                    }
                }
            }
        );   
      }