Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple onActivityResult for 1 Activity

Tags:

android

So I have a very simple app I am working on. It's purpose is to collect asset data from 1 pc, and 1 or 2 monitors. My form contains 3 edittext views, and 3 buttons (one for each asset I am collecting data for). The buttons invoke startActivityForResult for the barcode scanner, then I want to pass the result to the associated edittext view based on which button was pressed (example: press "scan" button to the right of "Asset - PC" edittext, scan and return data to it's associated edittext. Then if you press the button "scan" thats next to the "Asset - Mon1" edittext, return data to "Asset - Mon1" edittext.... and so on...)

With the code I have here, all the items work, just not as intended. Pressing any of the "scan" buttons always returns the result to the first edittext view "Asset - PC".

public class TestShit extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

    public void assetPcClick(View view) {
        Intent intent1 = new Intent("com.google.zxing.client.android.SCAN");
        intent1.setPackage("com.google.zxing.client.android");
        intent1.putExtra("SCAN_MODE", "ONE_D_MODE");
        startActivityForResult(intent1, 0);
    }   

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents1 = intent.getStringExtra("SCAN_RESULT");
                String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
                EditText assetPC = (EditText) findViewById(R.id.assetPC);
                assetPC.setText(contents1);
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }

    public void assetMon1Click(View view) {
        Intent intent2 = new Intent("com.google.zxing.client.android.SCAN");
        intent2.setPackage("com.google.zxing.client.android");
        intent2.putExtra("SCAN_MODE", "ONE_D_MODE");
        startActivityForResult(intent2, 0);
    }   

    public void onActivityResult2(int requestCode, int resultCode, Intent intent2) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents2 = intent2.getStringExtra("SCAN_RESULT");
                String format2 = intent2.getStringExtra("SCAN_RESULT_FORMAT");
                EditText assetMon1 = (EditText) findViewById(R.id.assetMon1);
                assetMon1.setText(contents2);
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }

    public void assetMon2Click(View view) {
        Intent intent3 = new Intent("com.google.zxing.client.android.SCAN");
        intent3.setPackage("com.google.zxing.client.android");
        intent3.putExtra("SCAN_MODE", "ONE_D_MODE");
        startActivityForResult(intent3, 0);
    }   

    public void onActivityResult3(int requestCode, int resultCode, Intent intent3) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents3 = intent3.getStringExtra("SCAN_RESULT");
                String format3 = intent3.getStringExtra("SCAN_RESULT_FORMAT");
                EditText assetMon2 = (EditText) findViewById(R.id.assetMon2);
                assetMon2.setText(contents3);
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }

}

Any suggestions on how I can better manage my multiple "ActivityForResult" and "onActivityResult" 's ?


My fix, thank you for all your help!

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents1 = intent.getStringExtra("SCAN_RESULT");
                String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
                EditText assetPC = (EditText) findViewById(R.id.assetPC);
                assetPC.setText(contents1);
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                String contents1 = intent.getStringExtra("SCAN_RESULT");
                String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
                EditText assetMon1 = (EditText) findViewById(R.id.assetMon1);
                assetMon1.setText(contents1);
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
        if (requestCode == 2) {
            if (resultCode == RESULT_OK) {
                String contents1 = intent.getStringExtra("SCAN_RESULT");
                String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
                EditText assetMon2 = (EditText) findViewById(R.id.assetMon2);
                assetMon2.setText(contents1);
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }    
like image 308
Devin Avatar asked Feb 17 '11 14:02

Devin


3 Answers

In your startActivityForResult, don't use 0's on both calls... use different numbers like 0 & 1... then you can implement a switch in your onActivityResult method with the requestCode. If the requestCode = 0 then the first method has returned, if it is 1, then the second has returned. This should be the same for more calls.

public void onActivityResult(int requestCode, int resultCode, Intent intent){
    switch(requestCode){
        case 0: // Do your stuff here...
        break;
        case 1: // Do your other stuff here...
        break;
        case etc:
        break;
    }
}

The calls should be something like this then: (For the first time)

startActivityForResult(intent1, 0);

(For the second time)

startActivityForResult(intent2, 1);

(for the third time)

startActivityForResult(intent3, 2);

(for the nth time)

startActivityForResult(intentn, n - 1);

Or, you could declare static int values to use, instead of the more unrecognisable int values.

like image 79
ThaMe90 Avatar answered Nov 19 '22 21:11

ThaMe90


While you startActivityForResult you send a requestcode with it, this should be different(unique) for your every activity you are starting from your button, say button 1 starts activity request code 1, button 2 requestcode = 2, and button 3 request code =3, then for your parent activity you must have only one onActivityResult() in this function take a switch case , scan requestcodes, requestcode = 1 will give result from first activity and request code =2 gives for activity 2 and so on...

like image 22
om252345 Avatar answered Nov 19 '22 21:11

om252345


There's nothing in Android that is ever going to recognize and call methods named onActivityResult2 or onActivityResult3. Those are just method names you made up that are going to be ignored by the system.

You need to change your code such that you pass a different request code when you call startActivityForResult(). (requestCode is the 2nd parameter to that method)

Then in onActivityResult check the requestCode to see which activity you are getting the result from, and handle accordingly.

like image 3
Mark B Avatar answered Nov 19 '22 20:11

Mark B