0

I know that this question may be asked someone else but I can't find a solution that worked for me. Basically I have a fragment with one button. When user press that button new activity is opened (that activity just scans qr code) and I want to pass scan result back to my fragment. So basically

public class UiFragment extends Fragment implements ServiceCallBack {
    // tons of code
    scanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ScanActivity.class); // start scan activity
                startActivity(intent);
            }
        });
}

public class ScanActivity extends AppCompatActivity {
   // lots of code
     public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (scanResult != null) {
            String result = scanResult.getContents();
            if(result != null && result.length() > 0){
                // now I want to go back to my previous fragment and pass result
            }
        }
        finish();
    }
}

Please note that I want activity to fragment communication! I have tried to use interface, however that doesn't worked for me. Is it possible to do that?

David
  • 3,055
  • 4
  • 30
  • 73

1 Answers1

0

If I understood your question correctly, then you must be having 1 activity which launches a fragment. Let's name that activity as MainActivity. Now change your UiFragment to look like

public class UiFragment extends Fragment implements ServiceCallBack {
// tons of code

public static final SCAN_REQUEST_CODE = 1001; //Any number

    scanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ScanActivity.class); // start scan activity
                startActivityForResult(intent, SCAN_REQUEST_CODE);
            }
        });
}

Your MainActivity will have onActivityResult()

public class MainActivity extends AppCompatActivity {
   // lots of code
     public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode == UiFragment.SCAN_REQUEST_CODE){
//Check for result code if you want to
if(resultCode == RESULT_OK){
        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (scanResult != null) {
            String result = scanResult.getContents();
            if(result != null && result.length() > 0){


//Now check if your fragment is visible
 UiFragment uiFragment = (UiFragment) getSupportFragmentManager().findFragmentByTag(your_fragment_tag_name);
                if (uiFragment != null && uiFragment.isAdded()) {
//Call your fragment's public method here. let's say you have a method update in your fragment then write
                    uiFragment.update(); // you can pass arguments in this method depending on your requirements.
                }
                }
            }

        }
    }
    }
    }

Your ScanActivity will not have onActivityResult() method

    public class ScanActivity extends AppCompatActivity {
       // lots of code

//Some method which scans and setResult
         public void scan() {
         setResult(RESULT_OK);
            finish();
        }
    }
Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45