Search code examples
androidmultithreadingimage-processingandroid-intentrunnable

Getting thread result in another activity for Android


My Main Activity invokes ResultActivity and at the same time invokes a Runnable thread. I want that Result activity doesn't wait for some part of result processing (Image) which may be provided later by thread and UI may be updated accordingly.

What I've tried to do is given below.

MainActivity (below method is actually a callback from another thread):

@Override
public void onCreate(Context context) {
    resultImageProcessor = new ResultImageProcessor();
    resImgProThread = new Thread(resultImageProcessor);
}

@Override
public void onBarcodeDetected(final Barcode barcode) {
    resultImageProcessor.setCameraBarcode(mCameraSource,barcode);
    resImgProThread.start();

    Intent intent = new Intent(this, ResultActivity.class);
    intent.putExtra(BarcodeObject, barcode);
    intent.putExtra(ResultCode, CommonStatusCodes.SUCCESS);
    intent.putExtra(ResImgProcObject, resultImageProcessor);
    startActivity(intent);
}

Result Image Processor:

public class ResultImageProcessor implements Serializable, Runnable {

    private ResultActivity resultActivityContext;

    ResultImageProcessor(){
        this.resultActivityContext = null;
    }

    public void setResultActivity(ResultActivity resultActivity) {
        this.resultActivityContext = resultActivity;
    }

    public void setCameraBarcode(CameraSource cameraSource, Barcode barCode){
        mCameraSource = cameraSource;
        barcode = barCode;
    }

    @Override
    public void run() {
        String picPath = ProcessImage(Obj..Attributes);
        //wait until result activity context is not set
        while(resultActivityContext == null){
            try {
                sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        resultActivityContext.onImageSaved(picPath);
    }
}

Result Activity:

protected void onCreate(Bundle savedInstanceState) {
    //...
    data = getIntent();
    Barcode barcode = data.getParcelableExtra(MainActivity.BarcodeObject);
    ResultImageProcessor resultImageProcessor = data.getParcelableExtra(MainActivity.ResImgProcObject);
    resultImageProcessor.setResultActivity(this);
}

//called from Result image processor
public void onImageSaved(String imagePath){
    ImageView barImgView = findViewById(R.id.barcode_image);
    Bitmap barcodeImage = BitmapFactory.decodeFile(imagePath);
    barImgView.setImageBitmap(barcodeImage);
    barImgView.invalidate();
}

With the above code, after invoking resultImageProcessor.startProcessing(), result activity is not launched nor runnable's run() method keeps busy in while loop. I traced them using logger. When I skip threading and pass image path to activity, everything goes fine beside being slow for activity switching.

Please indicate the problem or suggest better solution.


Solution

  • It turned out that the problem was in passing ResultImageProcessor object to ResultActivity intent as Parcelable. I followed a simple path of declaring resultActivityContext as static in ResultImageProcessor.

    public class ResultImageProcessor implements Runnable {
        public static ResultActivity resultActivityContext;
        ...
        @Override
        public void run() {
            ...
            resultActivityContext.onImageSaved(picPath);
            resultActivityContext = null;
        }
    }
    

    and in ResultActivity:

    ResultImageProcessor.resultActivityContext = this;