Search code examples
androidandroid-fragmentsandroid-camera-intent

Camera Intent in Fragment


I have a problem with using the camera intent in Fragment. I have an Activity which contains fragments, and I have four fragments. In my 3rd fragment I have call the camera Intent. After take a picture, the app doesn't resume only on the 3rd fragment, but it also appear the 1st fragment. You can see the pictures and the code.

Thanks for any solutions.

Code:

public class CreateAttachment extends Fragment{

private RelativeLayout btnBack, btnNext;
private LinearLayout btnAttachment, btnCapture;

private static final int CAMERA_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.create_property_attachment, container, false);

    btnBack = (RelativeLayout) view.findViewById(R.id.attachment_btnBack);
    btnBack.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            getFragmentManager().popBackStack();
        }
    });

    btnNext = (RelativeLayout) view.findViewById(R.id.attachment_btnNext);
    btnNext.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            nextFragment();
        }
    });

    btnAttachment = (LinearLayout) view.findViewById(R.id.btnAttachment);
    btnAttachment.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) { 
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.putExtra("return-data", true);
            startActivityForResult(Intent.createChooser(intent, "Complete action using"), MEDIA_TYPE_IMAGE);
        }
    });

    btnCapture = (LinearLayout) view.findViewById(R.id.btnCameraAccess);
    btnCapture.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
        }
    });

    return view;
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == Activity.RESULT_OK && requestCode == MEDIA_TYPE_IMAGE){

        Toast.makeText(getActivity(),"Photo is Chosen",Toast.LENGTH_SHORT).show();
    }
    else if (requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {

        Toast.makeText(getActivity(),"Photo is Captured",Toast.LENGTH_SHORT).show();
    }
  }
}                                        

Images: Here is my screenshot for 1st fragment, 3rd fragment, and the issue after take a picture by Camera Intent in the 3rd fragment.


Solution

  • Thanks for all of your answer. Anyway I don't know exactly what the error is, but the way to fix this is to override these two functions of the Activity on my 3rd fragment.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(getActivity(), "Work on onCreate when return into Fragment.", Toast.LENGTH_SHORT).show();
    }
    
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }