2

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.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Php Pete
  • 762
  • 3
  • 14
  • 29

1 Answers1

4

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:

Community
  • 1
  • 1
Than
  • 2,759
  • 1
  • 20
  • 41
  • Thank Than. So I understand correctly, I should add onActivityResult() to the activity calling the fragment. It is in this activity that I can retrieve the results from the barscan application? – Php Pete Jul 11 '14 at 14:07
  • 1
    Well it depends where do you want to handle your response. If inside fragment, do what i wrote up here, If inside activity call it by getActivity().startActivityForResult and handle it in activity. – Than Jul 11 '14 at 14:12