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();
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…