Questions about transfer values between the two Activity, we must first find out the activity that is the parent of two, that is a subclass, the parent class is usually the result you want, and let the child to help him deal with class, done after the results back passed to the parent class, my parent is ZxingActivity.java, and the child class is SimpleCaptureActivity.java. I have to scan two-dimensional code, for example briefly about it. Please take the comments in the order of the core code, you will understand.
First look at the parent class core code ZxingActivity.java in.
void public the onClick (View V) { Switch (v.getId ()) { // Open the scanned block Case R.id. btn_switch : changeLight () ; .. 1 // parent pack data, to assign a task subclass, I need What results, the Bundle the bundle = new new the Bundle () ; bundle.putString ( "Data" , "Data" ) ; the Intent Intent = new new the Intent (ZxingActivity. the this, SimpleCaptureActivity. class ) ; intent.putExtras (the bundle) ; // 2. when I finished telling subclass message telling subclass I need to return results, and therefore by the startActivityForResult () method, this time the request is sent to the subclass of the startActivityForResult (Intent , 001 ) ; BREAK;
See core code in SimpleCaptureActivity.java, sequential code execution order, please refer to the notation of
@Override protected void handleResult(final String resultString) { if (TextUtils.isEmpty(resultString)) { Toast.makeText(mActivity, io.github.xudaojie.qrcodelib.R.string.scan_failed, Toast.LENGTH_SHORT).show(); restartPreview(); } else { if (mDialog == null) { mDialog = new AlertDialog.Builder(mActivity) .setMessage (ResultString) .setPositiveButton ( " OK " , new new DialogInterface.OnClickListener () { @Override public void the onClick (DialogInterface Dialog , int Which) { //. 3. subclasses of the parent class problem received, the Intent intent1 its getIntent = ( ) ; // 4. begin to answer his father's questions, his answers to the parent class of the Intent intent1.putExtra ( "myresuly" , ResultString ) ; . // 5 subclass and the results of its own response code set by setResult ( 002, intent1) returned to the parent class, // this time the parent will need to be rewritten the onActivityResult () method to display the results setResult ( 002 , intent1) ; Finish () ; } }) .create () ;
In this case, the subclass completed the task of the parent class, then the parent will inspection, and look to the subclass is not the result he wanted, so we went back to the parent class ZxingActivity.java by weight write onActivityResult () method to a subclass of acceptance to the results.
//6.处理子类带回来的结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==REQUEST_OK && resultCode==RESPONSE_OK){ Bundle bundle=data.getExtras(); String dataResult=bundle.getString("myresuly"); Log.i("res","拿到结果了吗?"+dataResult); mQRCodeResult.setText(bundle.getString("myresuly")); } }
这样就搞定了两个Activity之间的传值问题,希望能够帮到你哦,我是小萝莉。