0

I have app, which turn on camera QR reader onClick in Fragment, after that it turns on ServiceActivity and when you click FAB it turns on TeaDialogFragment, where you click button scan QR it turns on camera QR reader and it should give result of QR to new Activity (TeaFromQrActivity). But here is a problem - method onActivityResult doesn't called anyway.

TeaDialogFragment.java

public class TeaDialogFragment extends DialogFragment {
public static int BARCODE_READER_REQUEST_CODE = 2;
String qrTeaCode;
private String TAG = TeaDialogFragment.class.getSimpleName();

public TeaDialogFragment() {
    // Empty constructor required for DialogFragment
}

public static TeaDialogFragment newInstance(String title) {
    TeaDialogFragment frag = new TeaDialogFragment();
    Bundle args = new Bundle();
    args.putString("title", title);
    frag.setArguments(args);
    return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    String title = getArguments().getString("title");
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    alertDialogBuilder.setTitle(title);
    alertDialogBuilder.setMessage("Message");
    alertDialogBuilder.setPositiveButton("scan QR", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
//  This code works - it turns on new Activity
//                Intent i = new Intent(getActivity().getApplicationContext(), TeaFromQrActivity.class);
//                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                getActivity().getApplicationContext().startActivity(i);

//  This code doesn't works - it doesn't turn on newActivity, when QR Reader have result, and it turn on current Activity
                Intent teaDialogIntent = new Intent(getActivity().getApplicationContext(), BarcodeCaptureActivity.class);
                teaDialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivityForResult(teaDialogIntent, BARCODE_READER_REQUEST_CODE);

        }
    });
    alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (dialog != null) {
                dialog.dismiss();
            }
        }

    });

    return alertDialogBuilder.create();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == BARCODE_READER_REQUEST_CODE) {
        if (resultCode == CommonStatusCodes.SUCCESS) {
            if (data != null) {
                Barcode barcode = data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject);
                Intent testIntent = new Intent(getActivity().getApplicationContext(), TeaFromQrActivity.class);
                qrTeaCode = "teaQrCode";
                testIntent.putExtra(qrTeaCode, barcode.displayValue);
                // Start the new activity
                startActivity(testIntent);
                Log.d(TAG, "Barcode read: " + barcode.displayValue);
            } else {
                Log.d(TAG, "No barcode captured, intent data is null");
            }
        } else {
            Log.e(TAG, String.format(getString(R.string.barcode_error_format),
                    CommonStatusCodes.getStatusCodeString(resultCode)));
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
}

TeaFromQrActivity.java

public class TeaFromQrActivity extends AppCompatActivity {
private String teaQR;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tea_from_qr);

    teaQR = getIntent().getExtras().getString("teaQrCode", "null");
    Toast.makeText(TeaFromQrActivity.this, "Text from qr: " + teaQR, Toast.LENGTH_SHORT).show();
}
}

Please tell me where I'm wrong.

mkal91
  • 11
  • 1
  • 3

1 Answers1

0

launch barcode activity like this

        Intent intent = new Intent(this, BarcodeCaptureActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivityForResult(intent, BARCODE_READER_REQUEST_CODE);

more detail refer BarcodeCaptureActivity,barcodereader

sasikumar
  • 12,540
  • 3
  • 28
  • 48
  • In DialogFragment doesn't work Intent intent = new Intent(this, BarcodeCaptureActivity.class); because of "this" – mkal91 Feb 08 '18 at 11:09
  • if its fragment use getActivity().getApplicationContext(); – sasikumar Feb 08 '18 at 11:11
  • yes, I have code like this: Intent teaDialogIntent = new Intent(getActivity().getApplicationContext(), BarcodeCaptureActivity.class); teaDialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getActivity().startActivityForResult(teaDialogIntent, BARCODE_READER_REQUEST_CODE); but problem is that startActivityForResult doesn't work – mkal91 Feb 08 '18 at 11:15
  • read this https://stackoverflow.com/questions/13825254/call-startactivityforresult-from-fragment-doesnt-call-onactivityresult – sasikumar Feb 08 '18 at 11:19