问题
I have created an application that presents the user with a dialog fragment with two options: Yes
and No
.
Should the user select 'Yes', the app will call a Zxing barscan application that is installed on the device, and return the result.
Now, I have a proof of concept for this working. However this proof of concept uses a simple Activity. Therefore I can achieve this Barscan result using activity, not with Dialog Fragment.
The code used in the proof of concept is as follows:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText("CONTENT: " + scanContent);
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
This code will not work with a dialog fragment. I have been searching and came upon this question.
However it will not integrate, as I cannot access the requestCode, resultCode, or intent required, and am quite confused as to how to do so.
回答1:
You fragment should launch activity by startActivityForResult
not getActivity().startActivityForResult
In your activity in onActivityResult
call super.onActivityResult
and in your fragment on onActivityResult
you can put your code which you have posted here.
More info why:
- onActivityResult is not being called in Fragment
- Where should onActivityResult be handled, dialog fragment, fragment, or activity?
来源:https://stackoverflow.com/questions/24699207/android-dialog-fragments-and-onactivityresult