Slo
Slo

Reputation: 25

Sending result of zxing barcode scanner in another class to Main class and activity

I'm new to Java and Android so some errors might be basic.

I have created two classes: Rdiet_Main.java and Rdiet_Zxing.java. Everything used to be in the same class but I wanted to keep one functionality per class.

When the scan button is pressed this happens in Rdiet_Main:

Rdiet_Zxing zxScan = new Rdiet_Zxing();
   zxScan.acquireBarcode(this);

Then it goes in the Rdiet_Zxing:

public void acquireBarcode(Rdiet_Main t){

    IntentIntegrator scanBarcodeIntegrator = new IntentIntegrator(t);
    scanBarcodeIntegrator.initiateScan();
}
public String onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanBarcodeResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if(scanBarcodeResult != null){
        String strResult;
        strResult = scanBarcodeResult.getContents();
        return strResult;
    }
    else{
        return null;
    }
}

I want to send the barcode to Rdiet_Main. How should I change my code to make it possible?

EDIT :

I now use startActivityForResult. However the scanner isn't launched when the button is pressed and the app just crashes (Unable to find explicit Activity Class in AndroidManifest.xml) . I think Rdiet_Zxing needs to be linked to a CaptureActivity or something similar.

Here's what's inside my AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Rdiet_Main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

EDIT2 :

Updated Rdiet_Zxing.java:

public class Rdiet_Zxing extends Activity{

    public static final int SUCCESS_RETURN_CODE = 1;
    public static final int FAILURE_RETURN_CODE = 2;

    public void acquireBarcode(Rdiet_Main t){

        IntentIntegrator scanBarcodeIntegrator = new IntentIntegrator(t);
        scanBarcodeIntegrator.initiateScan();
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        IntentResult scanBarcodeResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if(scanBarcodeResult != null){
            String strResult;
            strResult = scanBarcodeResult.getContents();
            //SOL1
            Intent intentResult = new Intent();
            Bundle b = new Bundle();
            b.putString("myResult", strResult);
            intentResult.putExtras(b);
            setResult(SUCCESS_RETURN_CODE, intentResult);
            finish();
            //-------------
        }
        else{
            setResult(FAILURE_RETURN_CODE, null);
            finish();
        }
    }
}

Updated Rdiet_Main.java:

//Scan button
                    Intent i = new Intent(Rdiet_Main.this, Rdiet_Zxing.class);
                    startActivityForResult(i, SUB_ACTIVITY_REQUEST_CODE);

    //ScanResult

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == SUB_ACTIVITY_REQUEST_CODE) {
                Bundle b = data.getExtras();
                strBarcode = b.getString("myResult");
                tbxAPIbarcode.setText(strBarcode);

            }
        }

Upvotes: 1

Views: 1856

Answers (2)

Cagri Yalcin
Cagri Yalcin

Reputation: 402

You can do all these events in the same class or you can use 'sub activity'.

Edit: Here is example

In your main activity, when you press the button:

Intent i = new Intent(Rdiet_Main.this, Rdiet_Zxing.class);
startActivityForResult(i, SUB_ACTIVITY_REQUEST_CODE);

Then, you will start the scanner. When you get your result:

public String onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanBarcodeResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if(scanBarcodeResult != null){
    String strResult;
    strResult = scanBarcodeResult.getContents();
    //Add these lines
    Intent intent = new Intent();
    Bundle b = new Bundle();
    b.putString("myResult", strResult);
    intent.putExtras(b);
    setResult(SUCCESS_RETURN_CODE, intent);
    finish();
    //-------------
    return strResult;
}
else{
    return null;
}

}

Also in scanner activity add this as global:

public static final int SUCCESS_RETURN_CODE = 1;

And in main activity:

private static final int SUB_ACTIVITY_REQUEST_CODE = 100;

Finally, to get your result in main:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SUB_ACTIVITY_REQUEST_CODE) {
        Bundle b = data.getExtras();
        String result = b.getString("myResult");
    }
}

Edit2:

When you create a new activity it must be added to your manifest but if it's not, you can add like this,

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Rdiet_Main">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<!-- added here -->
<activity android:name=".Rdiet_Zxing"></activity>
</application>

Hope it helps.

Upvotes: 0

Harminder Singh
Harminder Singh

Reputation: 332

  • create a callback like below

    public interface CallbackToMain {
    
    void result(String scanvalue);
    
    }  
    
  • Implement it on your Redit_Main Activity.

  • create object of Rdiet_Zxing class inside Redit_Main and pass interface reference.

    Rdiet_Zxing zxScan = new Rdiet_Zxing(this); // this refered to callback interface.
    
    zxScan.acquireBarcode();
    
  • Inside Redit_Zxing do things like below.

     CallbackToMain callback;
     public void Rdiet_Zxing(CallbackToMain callback){
     this.callback=callback;
     }
    
    
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
    
     callback.result("your scanned value");
     }
    

Upvotes: 1

Related Questions