1

I'm calling ACTION IMAGE CAPTURE intent from a fragment, and I've implemented the onActivityResult() in same fragment but the onActivityResult is not triggered while running it with my note2 device. Check the code snippets below that I used.

It worked fine when I am not using

 import android.support.v4

  • Activity which I open fragment

          public class HomeActivity extends FragmentActivity {
    
  • Fragment which I call Camera Intent

         public class SaveCardFragment extends Fragment { 
         private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1888;
         ...
    
  • Calling Camera Intent

         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
         startActivityForResult(intent,CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
         ...
    
  • Function to detect activity result

        @Override 
        public void onActivityResult(int requestCode, int resultCode, Intent data) { 
        super.onActivityResult(requestCode, resultCode, data); 
        Log.d("Result", ""+requestCode);
        }
    
  • Configuration in AndroidManifest

         <uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="18" />
         <uses-permission android:name="android.permission.CAMERA"/>
         <uses-feature android:name="android.hardware.camera"/>
         <application
         android:allowBackup="true" android:icon="@drawable/ic_launcher" 
         android:label="@string/app_name" android:theme="@style/AppTheme"
         >
         <activity android:name=".HomeActivity" android:label="@string/title_activity_home"/>
    
1'hafs
  • 559
  • 7
  • 25

1 Answers1

1

I'm calling ACTION IMAGE CAPTURE intent from a fragment (...)

That's not quite true. Look at the following line of code:

getActivity().startActivityForResult(SaveCardFragment.this,intent,CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

Key to notice is that you call startActivityForResult() on the hosting activity (HomeActivity), not the fragment (SaveCardFragment), hence the result is delivered to the activity and not propagated any further. If you want the result to be delivered to the fragment, ensure you call startActivityForResult() on the fragment. In other words, just get rid of the getActivity() prefix:

startActivityForResult(SaveCardFragment.this,intent,CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

See: Fragment#startActivityForResult

MH.
  • 45,303
  • 10
  • 103
  • 116
  • I've made corrections to my code but still onActivityResult is not triggered – 1'hafs Aug 31 '14 at 10:23
  • @hara: Are you instantiating and adding the `SaveCardFragment` dynamically (in code) or is it statically declared in a layout? If the former applies, make sure you're not accidentally re-adding/replacing the originally calling fragment. Perhaps you could add some more code that shows how you "open" your fragment? – MH. Aug 31 '14 at 10:39
  • @hara: another thought: the parameters of your `startActivityForResult()` call don't seem right. What's that first parameter (`SaveCardFragment.this`) doing there? All you should need is the intent and a request code. You didn't declare your own `startActivityForResult()` method by any chance, did you? – MH. Aug 31 '14 at 10:44
  • I've corrected the code I'm using just an intent and request code in that function. My Fragment is statically declared one, and I did not declare startActivityForResult() method. What else I can add while starting the fragment? – 1'hafs Aug 31 '14 at 11:13
  • fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager .beginTransaction(); transaction.replace(R.id.frame_container, fragment, tag); transaction.addToBackStack(tag).commit();//this code I use to add //fragment – 1'hafs Aug 31 '14 at 11:17
  • @hara: sounds like you have two fragment instances on top of each other. If your fragment is statically declared, don't add another one using a transaction. Do one, not both. Besides, you can't replace a statically declared fragment. The fact that you 'solved' it using an activity suggests that the fragments are probably not set up correctly. – MH. Sep 01 '14 at 00:07