DialogFragment中的startActivityForResult - QR阅读器

时间:2018-02-08 10:40:01

标签: java android android-dialogfragment

我有app,打开相机QR阅读器onClick in Fragment, 之后它打开ServiceActivity 当你点击FAB它打开TeaDialogFragment,你点击按钮扫描QR它打开相机QR阅读器 它应该将QR的结果给新的活动(TeaFromQrActivity)。但这是一个问题 - 无论如何,onActivityResult方法都没有被调用。

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();
}
}

请告诉我我哪里错了。

1 个答案:

答案 0 :(得分:0)

像这样启动条形码活动

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

更多详情请参阅BarcodeCaptureActivitybarcodereader