Home > database >  'startActivityForResult(android.content.Intent, int)' is deprecated what can i do?
'startActivityForResult(android.content.Intent, int)' is deprecated what can i do?

Time:01-17

'startActivityForResult(android.content.Intent, int)' is deprecated what can i do? This is the code for my qr code scanner android app (java) (androidstudio):

package com.example.wfr;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_CODE = 0;

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


        //Button click event to open QR scanner
        findViewById(R.id.camera_button).setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, QRCodeScanner.class);
            intent.putExtra("SCAN_FORMATS", "QR_CODE");
            startActivityForResult(intent, REQUEST_CODE);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            if (data != null) {
                String scannedText = data.getStringExtra("com.journeyapps.barcodescanner.CaptureActivity.SCAN_RESULT");
                TextView scannedTextView = findViewById(R.id.scanned_text);
                scannedTextView.setText(scannedText);
            }
        }
    }
}

CodePudding user response:

A basic training is available at https://developer.android.com/training/basics/intents/result

Here is an example on how to convert the existing code with the new one:

The old way:

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

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK && requestCode == 123) {
        doSomeOperations();
    }
}

The new way (Java):

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

// 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>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    // There are no request codes
                    Intent data = result.getData();
                    doSomeOperations();
                }
            }
        });

CodePudding user response:

If a function is deprecated, the documentation usually tells you why and what you should use instead:

This function is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your activity. Use registerForActivityResult passing in a StartActivityForResult object for the ActivityResultContract.

The other answer shows you the developer guide for using this stuff and has an example, but it's important to always look at the documentation when you run into deprecated stuff - it's rare a function is removed with no information, and sometimes you won't need to worry about the deprecation anyway (e.g. if it only happened with the newest version of Android, meaning they've just decided it will be removed sometime in the future as the older APIs fall out of use)

  • Related