Android在Fragment中使用onActivityResult

时间:2015-04-16 14:23:06

标签: android

我想简单地将zxing条形码扫描器用于项目中,对于此操作,我使用this tutorial将条形码阅读器实现到项目中,本教程使用两个Zxing类进入项目项目,在我的应用程序中我没有活动,我使用Fragment,遗憾的是我无法从onActivityResult获取数据以获取条形码的扫描内容:

public class FragmentAddNewWayBill extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        G.currentActivity = FragmentAddNewWayBill.this.getActivity();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_new_waybill, container, false);

        view.findViewById(R.id.imgv_scan_barcode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentIntegrator scanIntegrator = new IntentIntegrator(G.currentActivity);
                scanIntegrator.initiateScan();
            }
        });
        return view;
    }
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (scanningResult != null) {
            String scanContent = scanningResult.getContents();
            String scanFormat = scanningResult.getFormatName();
            Log.e("barcode is: ", scanContent);
            Toast.makeText(G.currentActivity,scanContent, Toast.LENGTH_SHORT).show();
        }
        else{
            Log.e("No scan data received! ", "");
            Toast toast = Toast.makeText(G.currentActivity,"No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }
    }
    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}

2 个答案:

答案 0 :(得分:0)

实施将取决于如何调用startactivityforresult。 希望startactivityforresult从库中调用,并调用它来自活动。 所以requestCode,resultCode将在onActivityResult的活动中正确。 因此,将onActivityResult的代码移动到Activty,一切正常。

如果调用了getActivity()。startactivityforresult,那么它将从Fragment调用。然后onActivityResult应该工作正常。

答案 1 :(得分:0)

我也遇到了ZXing条形码阅读器的问题 如果你需要扫描QR码我会重新装配Qr-Droid API非常简单,你可以从片段中启动它没有问题

    public class SrnSuche extends Fragment {
        Context context;
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

           final View rootView = inflater.inflate(R.layout.fragment_srnsuche, container, false);

            Button Button_such = (Button) rootView.findViewById(R.id.button_suchen);
            Button Button_scan = (Button) rootView.findViewById(R.id.button_scan);
            context = rootView.getContext();

            Button_scan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {




                    Intent qrDroid = new Intent( "la.droid.qr.scan" );
                    qrDroid.putExtra( "la.droid.qr.complete" , true);
                    try {
                    startActivityForResult(qrDroid, 0);
                        //ja=true;
                    } catch (ActivityNotFoundException activity) {
                      //  ja=false;
                        qrDroidRequired(getActivity());
                    }
                }
            });
    }
            return rootView;
       public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
        /*
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (scanningResult != null) {
            String scanContent = scanningResult.getContents();
            Toast toast = Toast.makeText(context,
                "SCAN THERE "+scanContent, Toast.LENGTH_SHORT);
            Intent i = new Intent(context, Historie_Activity.class);
            i.putExtra("value_seriennummer", scanContent);
            startActivity(i);
            toast.show();


        }else{
            Toast toast = Toast.makeText(context,
                    "No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }*/

if (intent!= null){
        String result = intent.getExtras().getString("la.droid.qr.result");
        if (result != null) {

            Toast toast = Toast.makeText(context,
                    "SCAN THERE "+result, Toast.LENGTH_SHORT);
            Intent i = new Intent(context, Historie_Activity.class);
            i.putExtra("value_seriennummer", result);
            startActivity(i);
            toast.show();


        }else{
            Toast toast = Toast.makeText(context,
                    "No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }
}
    }
 }

链接到QR-Droid API click