0

I am beginner to android..I Integrated qr scanner in my app..problem is I added qr scanner button in two fragment..for receiving result I added on activityResult method..In my mainactivity..Is any another method for receiving result in android Instead of OnactivityResult method..If not their I should I add multiple onActivityResult method In my Mainactivty..can anyOne help me..

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);
    }
    // }

}
pruthvi raj
  • 9
  • 1
  • 3

1 Answers1

2
static final int FRAGMENT_ONE_REQUEST = 1;  // The request code
static final int FRAGMENT_TWO_REQUEST = 2;  // The request code

then use this from fragment one

 startActivityForResult(intent, FRAGMENT_ONE_REQUEST);

then use this from fragment two

startActivityForResult(intent, FRAGMENT_TWO_REQUEST);

in each fragment

@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) {
       }
    }
}
Nilesh Deokar
  • 2,975
  • 30
  • 53
  • bro how can I use startActivityForResult in fragment..can you send me a full code – pruthvi raj May 11 '16 at 06:21
  • Why did you check requestCode in the first condition ? You either check if this is OK then manage the request code OR you manage the request code and the resultCode for each but here, if you came from FRAGMENT_TWO, this won't work. – AxelH May 11 '16 at 07:00
  • that was my bad. simple copy paste error. corrected! thanx! @AxelH – Nilesh Deokar May 11 '16 at 07:08
  • You are welcome, add the condition for Frag_two just to be complete and you have my vote. – AxelH May 11 '16 at 07:17
  • see my full codebutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { IntentIntegrator integrator = new IntentIntegrator(getActivity()); integrator.initiateScan(); this i added in fragment and onactivity result method i added in activity – pruthvi raj May 11 '16 at 07:42
  • yeah!. @pruthvireddy if this is working well then consider accepting answer! thanks! – Nilesh Deokar May 11 '16 at 08:47