Search code examples
javaandroidtransitionbarcodezxing

Android ZXing scanner activity transition not working


I'm using a simple activity (from this library) with ZXing to scan a barcode. Everything works fine, but the transition between the MainActivity and the ScannerActivity doesn't work well. Halfway through it gets interrupted. When going back from the ScannerActivity to the MainActivity the transition works like desired. Only while loading the barcodescanner it doesn't look really good.

Do you have any idea how to fix this?

MainActivity:

private void invokeScanner() {
    try {
        Intent intent = new Intent(this, ScannerActivity.class);
        stealFocus(et_loadInput);
        startActivityForResult(intent,0);
        this.overridePendingTransition(R.anim.detail_anim_up, R.anim.detail_anim_down);
    } catch (Exception e){
        Log.e(TAG, "couldn't open scanner");
        e.printStackTrace();
        openAlertDialog(Const.MSG_PROCESSING_ERROR);
    }
}

ScannerActivity:

public class ScannerActivity extends AbstractTitleBarActivity implements ZXingScannerView.ResultHandler{

private ZXingScannerView mScannerView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mScannerView = new ZXingScannerView(this);
    setContentView(mScannerView);

    this.tv_backTitle.setText(getString(R.string.TITLE_SEARCH_VIEW));
    this.tv_heading.setText("");

    TextView titleClose = (TextView) findViewById(R.id.btn_close);
    titleClose.setVisibility(View.VISIBLE);
    titleClose.setOnClickListener(titleCloseListener);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

@Override
public void onResume() {
    super.onResume();
    mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
    mScannerView.startCamera();          // Start camera on resume
}

@Override
public void onPause() {
    super.onPause();
    mScannerView.stopCamera();           // Stop camera on pause
}

@Override
protected boolean isBackBtnVisible() {
    return false;
}

@Override
protected boolean isLogoVisible() {
    return false;
}

@Override
public void handleResult(Result rawResult) {

    Log.v(TAG, rawResult.getText()); // Prints scan results
    Log.v(TAG, rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)
    MainActivity.scanVal = rawResult.toString();
    MainActivity.loadVal = MainActivity.scanVal;
    finish();
    overridePendingTransition(R.anim.detail_anim_back_down,R.anim.detail_anim_back_up);


}

// listener for close button
protected TextView.OnClickListener titleCloseListener = new TextView.OnClickListener() {
    public void onClick(View v) {
        finish();
        overridePendingTransition(
                R.anim.detail_anim_back_down, R.anim.detail_anim_back_up);
    }
};

}


Solution

  • I actually solved my own problem :D with this answer.

    I just put the ZXingScannerView into a fragment and added a surface view with 0px height as a sibling. Now the animation works fine and the barcode scanner loads without refreshing the complete activity.