Home > other >  Transfer data to onSuccess() method
Transfer data to onSuccess() method

Time:10-27

In the following code I write the result of a procedure into a Text field. The code is executed as part of a task.

I want “transfer” text (String “test”) to onSuccess() method, so that I can use it within onSuccess(). I do not want to do that via static / global variables.

My question is: can I transfer data to the onSucess() method?

private void recognizeTextFromImage(InputImage image) {

  String test = “Test“

  TextRecognizer recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS);
  Task<Text> task = recognizer.process(image);
  // recognizer.process(image)
  task
    .addOnSuccessListener(
      new OnSuccessListener<Text>() {
      @Override
      public void onSuccess(Text texts) {
        recognizedText = processTextRecognitionResult(texts);
        editText_Test.setText(test   recognizedText);
        Log.d(TAG,"Successful: "   recognizedText);
      }
    })

    .addOnFailureListener(
      new OnFailureListener() {

    }
  });
  recognizer.close();
}

CodePudding user response:

Variable "test" is not some kind of "global". It is a local variable by definition, and as such, a new one will be instantiated every time a caller starts the method. I can see no concurrency issue with just using the test variable within onSuccess, unless TextRecognizer is implemented in some really weird way.

  • Related