startActivityForResult() is depricated. Best Alternative of startActivityForResult() with best practice.

Here you find the best alternative of startActivityForResult() method which is deprecated in lasted android studio. startActivityForResult() is use to get the data from the another activity that is either image, files, videos, document or etc.

Now we see the little bit of old and new method uses and find the best suitable practice to use it.

Old method

Intent intent = new Intent();
    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == 123) {

Latest Method

// You can do the assignment inside onAttach or onCreate, i.e, before the activity is displayed
    ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                public void onActivityResult(ActivityResult result) {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        // There are no request codes
                        Intent data = result.getData();

    public void openSomeActivityForResult() {
        Intent intent = new Intent(this, SomeActivity.class);

Post a Comment

Previous Post Next Post

Recent Posts