Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

android - Multiple onActivityResult for 1 Activity

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
            }
        }
    }    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...