Good, I have the following code that works for me when I work with normal activities, the fact is that I am now making an app dependent on fragments, so I do not know how to make this request:
Code to read the qr
scanbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(PreScan.this, LeerQR.class);
startActivityForResult(intent, REQUEST_CODE);
}
});
This activity receives the values that result from the activity LeerQR.class
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_CODE && resultCode == RESULT_OK){
if(data != null){
final Barcode barcode = data.getParcelableExtra("barcode");
result.post(new Runnable() {
@Override
public void run() {
result.setText(barcode.displayValue);
Now, from leerQR send me the answer to my activity back:
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if(barcodes.size() > 0){
Intent intent = new Intent();
//String [] variables = barcodes.split(=);
intent.putExtra("barcode", barcodes.valueAt(0));
setResult(RESULT_OK, intent);
finish();
}
}
The question is, how can I do all this with fragment, how can I call the next activity (which is a fragment) and have the result returned to me in such a way that I can use it as it is already programmed in the activity.
Edit, this is my preScan fragment:
public class PreScan extends Fragment {
Button btnscan;
RequestQueue requestQueue;
TextView result;
String montooper, comCorreo;
public static final int REQUEST_CODE = 100;
public static final int PERMISSION_REQUEST = 200;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_pre_scan, container, false);
btnscan = (Button)view.findViewById(R.id.btn_scanear);
requestQueue = Volley.newRequestQueue(getActivity());
if(ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.CAMERA}, PERMISSION_REQUEST);
}
btnscan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Esto es lo que debo pasar al siguiente fragment, y recibir la respuesta para poder procesarla
// Intent intent = new Intent(PreScan.this, LeerQR.class);
// startActivityForResult(intent, REQUEST_CODE);
}
});
return view;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_CODE && resultCode == RESULT_OK){
if(data != null){
final Barcode barcode = data.getParcelableExtra("barcode");
result.post(new Runnable() {
@Override
public void run() {
result.setText(barcode.displayValue);
//aqui proceso el resultado. en una actividad funciona sin problema, aqui no, ya que no se como hacer con los fragments
}
});
}
}
}
}
This is where it should be processed:
public class LeerQR extends AppCompatActivity {
SurfaceView cameraView;
BarcodeDetector barcode;
CameraSource cameraSource;
SurfaceHolder holder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leer_qr);
cameraView = (SurfaceView) findViewById(R.id.cameraView);
cameraView.setZOrderMediaOverlay(true);
holder = cameraView.getHolder();
barcode = new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
if(!barcode.isOperational()){
Toast.makeText(getApplicationContext(), "Sorry, Couldn't setup the detector", Toast.LENGTH_LONG).show();
this.finish();
}
cameraSource = new CameraSource.Builder(this, barcode)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedFps(24)
.setAutoFocusEnabled(true)
.setRequestedPreviewSize(1920,1024)
.build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try{
if(ContextCompat.checkSelfPermission(LeerQR.this, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
cameraSource.start(cameraView.getHolder());
}
}
catch (IOException e){
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
barcode.setProcessor(new Detector.Processor<Barcode>() {
@Override
public void release() {
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if(barcodes.size() > 0){
Intent intent = new Intent();
//String [] variables = barcodes.split(=);
intent.putExtra("barcode", barcodes.valueAt(0));
setResult(RESULT_OK, intent);
finish();
}
}
});
}
}
What I want is to pass the result to the first fragment, to be able to process it there