我是android的初学者。我在我的应用程序中集成了qr扫描仪。问题是我在两个片段中添加了qr扫描仪按钮...为了接收结果我在activityResult方法上添加了...在我的主动作中..是任何其他方法接收结果在android而不是OnactivityResult方法..如果不是他们我应该添加多个onActivityResult方法在我的Mainactivty ..can anyOne帮助我..
below is the my code for onActivityresult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult scanResult =IntentIntegrator.parseActivityResult(requestCode, resultCode,
data);
if (scanResult != null) {
if (scanResult.getContents() == null) {
Log.d("ScanFragment", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, " " + scanResult.getContents(), Toast.LENGTH_SHORT).show();
}
} else {
//result fragment
super.onActivityResult(requestCode, resultCode, data);
}
// }
}
答案 0 :(得分:2)
static final int FRAGMENT_ONE_REQUEST = 1; // The request code
static final int FRAGMENT_TWO_REQUEST = 2; // The request code
然后从片段1中使用它
startActivityForResult(intent, FRAGMENT_ONE_REQUEST);
然后从片段二中使用它
startActivityForResult(intent, FRAGMENT_TWO_REQUEST);
每个片段中的
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK) {
//some code
if (requestCode == FRAGMENT_ONE_REQUEST) {
}
}
}