0

I have a ScanActivity from which I instantiate a class(Preview) which clicks images and sets a callback on the Camera object. If the operation on callback is successful, I want to return back to ScanActivity. Ex:

class Preview extends SurfaceView implements SurfaceHolder.Callback{
    Camera mCamera;
    Preview(){} //constructor to initialize mCamera
    public void start(){
      mCamera.startPreview();
      mCamera.setPreviewCallback(new Camera.PreviewCallback() {
        public void onPreviewFrame(byte[] data, Camera camera) {
             /* 
              if(scanSuccessful){
                  return to activity
                } 
            */
        }
    }
}

class ScanActivity extends Activity{

    public void onCreate(){
        Preview p = new Preview();
        preview.start();
    }
}

From the callback I want to return back to the calling activity(ScanActivity) in case of a successful scan. I know broadcast receiver is one way of achieving it, is there any other way? And what are the pros and cons of using them?

Infant Dev
  • 1,659
  • 8
  • 24
  • 48

2 Answers2

0

Few ideas, not necessarily full list - You might want to:

  1. start activity for result
  2. delegate work to service, if it it's time consuming
  3. use listener interface - how to create own listener interface in android.
  4. use Android's dialogs

Regarding question about pros and cons it is very subjective, and AFAIK against SO rules :)
However, I think that point 4 as being quite new addition to Android's SDK is a way to go. I'd also advice against point 3 - to my best knowledge this would run on UI thread, and in case of heavy duty work - might cause application to become unresponsive.

Community
  • 1
  • 1
brainovergrow
  • 458
  • 4
  • 13
0

brainovergrow is correct. You need a setResult method which allow you to send a result to parent activity. I think you will be helpful a Parcelable mechanism also to send data between activities.

Code :

public interface PreviewCallback {
    void onPreviewFrame(byte[] data, Camera camera);
}

class Preview extends SurfaceView implements SurfaceHolder.Callback{
    Camera mCamera;
    PreviewCallback listener;
    Preview(){} //constructor to initialize mCamera

    public void setCallback(PreviewCallback callback) {
        listener = callback;
    }

    public void start(){
      mCamera.startPreview();
      mCamera.setPreviewCallback(new Camera.PreviewCallback() {
        public void onPreviewFrame(byte[] data, Camera camera) {
              listener.onPreviewFrame(data,camera);
        }
    }
}

class ScanActivity extends Activity implements PreviewCallback {

    public void onCreate(){
        Preview p = new Preview();
        preview.setCallback(this);
        preview.start();
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        // do your work
    }
}
Community
  • 1
  • 1
Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
  • I don't want to pass data between activities, I know how to achieve that. I want to pass control from a plain class to an activity. I have edited the question, to explain it better. – Infant Dev Mar 25 '15 at 09:26