Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

java - onActivityResult() doesn´t work in my fragment

I′m trying to scan a QR code using a Button in my HomeFragment of a Navigation Drawer. I did it once in an activity, but when I try to run it in my Fragment, the onActivityResult() method just doesn′t work. I already called the super method in the Activity of the Navigation drawer. Hope someone could help me.

Here′s my Activity Code

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {

        super.onActivityResult(requestCode,resultCode,data);

    }

Here′s my Fragment Code

    @Override
    public void onClick(View v) {
       scanCode();

    }
    private void scanCode(){
        IntentIntegrator integrator = new IntentIntegrator(getActivity());
        integrator.setCaptureActivity(CaptureAct.class);
        integrator.setOrientationLocked(false);
        integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
        integrator.setPrompt("Scanning Code");
        integrator.setBeepEnabled(true);
        integrator.setBarcodeImageEnabled(true);
        integrator.initiateScan();
    }

 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data){
     super.onActivityResult(requestCode, resultCode, data );
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        String Auxiliar = null;
        if(result!= null){
            if(result.getContents() != null){
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getApplicationContext());
                Auxiliar = result.getContents();
                builder.setMessage(Auxiliar);
                builder.setTitle("Scanning result");

                String finalAuxiliar = Auxiliar;
                builder.setPositiveButton("See Again", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        scanCode();
                    }
                }).setNegativeButton("finish", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        getActivity().finish();
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
            else{
                Toast.makeText(getActivity(), "No Results", Toast.LENGTH_LONG).show();
            }
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...