OnBackPress()-使用ZBarScannerView的活动为空白

时间:2018-10-03 09:50:55

标签: android android-layout listview android-fragments

我已经使用 ZBarScannerView 在我的应用程序中实现了条形码扫描仪功能。

我有一个包含多个片段的活动,并且在一个片段上有一个扫描按钮。通过单击该按钮,我跳到扫描实现了 ZBarScannerView 代码的活动。

当我从扫描活动中获取条形码时,我将其跳回到包含条形码点击按钮的那个片段。然后,如果我从该片段按回来,它将显示空白屏幕。我该怎么办?

请回复

ScanActivity的代码:

    public class ScanActivity extends AppCompatActivity implements ZBarScannerView.ResultHandler {
    private ZBarScannerView mScannerView;
    private static final int REQUEST_CAMERA = 1;
    ArrayList<ModelProductDetail> modelProductArticleCodeList;
    DatabaseHelper databaseHelper;
    String article_code;

    ArrayList<ModelUnrecognisedCode> modelUnrecognisedCodeArrayList;
    ArrayList<ModelUnrecognisedCode> singleUnrecognisedCheck;

    //camera permission is needed.

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);

        databaseHelper = new DatabaseHelper(this);
        checkUserPermission();

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if (mScannerView == null) {
                        mScannerView = new ZBarScannerView(this);    // Programmatically initialize the scanner view
                        setContentView(mScannerView);
                    }
                } else {
                    Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
                    checkUserPermission();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        }

    }

    private void checkUserPermission() {
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
                return;
            }
        }
        loadImage();
    }

    private void loadImage() {
        if (mScannerView == null) {
            mScannerView = new ZBarScannerView(this);    // Programmatically initialize the scanner view
            setContentView(mScannerView);
        }
    }


    @Override
    public void onResume() {
        super.onResume();

        if (mScannerView instanceof ZBarScannerView) {
            mScannerView.startCamera();
            mScannerView.setResultHandler(this);
            // Stop camera on pause
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (mScannerView instanceof ZBarScannerView) {
            mScannerView.stopCamera();           // Stop camera on pause
        }
    }

    @Override
    public void handleResult(me.dm7.barcodescanner.zbar.Result result) {


        String resultCode = result.getContents();

        Log.e("TAG", "handleResult: " + resultCode);
        mScannerView.stopCamera();
        modelProductArticleCodeList = new ArrayList<>();
        modelProductArticleCodeList = databaseHelper.getProductByArtCode(resultCode);


        if (modelProductArticleCodeList.size() == 0) {

            Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(getApplicationContext().VIBRATOR_SERVICE);
            vibrator.vibrate(1000);


            singleUnrecognisedCheck = new ArrayList<>();

            singleUnrecognisedCheck = databaseHelper.getUnrecognisedByCode(resultCode);

            if (singleUnrecognisedCheck.size() == 0) {

                Intent in = new Intent(this, ContainAllFragmentsActivity.class);
                in.putExtra("unrecognised_alert", true);
                in.putExtra("unrecognised_code", resultCode);
                in.putExtra("jump", "1");
                in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(in);
                finishAffinity();
                int pid = android.os.Process.myPid();
                android.os.Process.killProcess(pid);
                Toast.makeText(this, "not matched code", Toast.LENGTH_SHORT).show();

            } else {
                Intent in = new Intent(this, ContainAllFragmentsActivity.class);
                in.putExtra("jump", "3");
                in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(in);
                finishAffinity();
                int pid = android.os.Process.myPid();
                android.os.Process.killProcess(pid);
                Toast.makeText(this, "Unrecognised code has already been added.", Toast.LENGTH_SHORT).show();
            }


        } else {

//            for (ModelProductDetail modelProductDetail : modelProductArticleCodeList) {
//                article_code = modelProductDetail.getArticle_code();
//            }

            Intent in = new Intent(this, ContainAllFragmentsActivity.class);

            in.putExtra("product_art", resultCode);
            in.putExtra("source_type", "art");
            in.putExtra("jump", "2");
            in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(in);
            finishAffinity();
            int pid = android.os.Process.myPid();
            android.os.Process.killProcess(pid);

        }
    }

    public void changeFragments(Fragment fragment) {
        FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }


    private void getDialog() {

        final Dialog d = new Dialog(this);
        d.setContentView(R.layout.dialog_unrecognised_code);
        d.setCanceledOnTouchOutside(true);
        d.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        final EditText etCode = (EditText) d.findViewById(R.id.etCode);
        final EditText etrecognisedComment = (EditText) d.findViewById(R.id.etrecognisedComment);
        ImageView imgClose = (ImageView) d.findViewById(R.id.imgClose);
        ImageButton ibSubmit = (ImageButton) d.findViewById(R.id.ibSubmit);

        imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                d.dismiss();
            }
        });


        ibSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                ModelUnrecognisedCode modelUnrecognisedCode = new ModelUnrecognisedCode();
                modelUnrecognisedCode.setUnrecognised_code(etCode.getText().toString());
                modelUnrecognisedCode.setUnrecognised_comment(etrecognisedComment.getText().toString());

                databaseHelper.addUnrecognisedCode(modelUnrecognisedCode);

                // changeFragments(new SearchProductFragment());

                modelUnrecognisedCodeArrayList = new ArrayList<>();
                modelUnrecognisedCodeArrayList = databaseHelper.getUnrecognisedCode();
                Log.e("TAG", "unrecognised code: " + modelUnrecognisedCodeArrayList.size());

                d.dismiss();

            }
        });


        d.show();


    }


}

1 个答案:

答案 0 :(得分:1)

我假设成功扫描条形码后,您正在获取产品数据,如果找到了数据,则您正在更换屏幕。因此,以此更改您的else语句,看看它是否有效

else {
          Intent in = new Intent(this, ContainAllFragmentsActivity.class);
          in.putExtra("product_art", resultCode);
          in.putExtra("source_type", "art");
          in.putExtra("jump", "2");
          startActivity(in);
          finish();
        }