0

I have two fragments FragmentA and FragmentB. I open FragmentB from FragmentA. In FragmentB I take the picture from camera and when I accepted picture, fragment which opened is FragmentA.

I open both fragemnt with replace.

code to repleace fragment

private void replaceFragment(Fragment newFragment, String tag) {
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fragment_container, newFragment, tag).addToBackStack(null).commit();
    }

create picture

    public void btnAddPhoto() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
REQUEST_IMAGE_CAPTURE);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

        }
    }

code to receive picture

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
            imageView.setImageBitmap(imageBitmap);

        }
    }

I would to stay with FragemntB because i want to set the picture from camera on view in FragemntB. At the moment it looks like FragemntA is upper than FrgamentB, then I return, opened is FragemntB, and at least FragmentA again.

Phantômaxx
  • 37,352
  • 21
  • 80
  • 110
  • I bet that the problem is in `onCreate` of `Activity` ... you are not checking if Activity is recreated or not and adding FragmentA there – Selvin Apr 19 '19 at 10:48
  • https://stackoverflow.com/questions/16014930/android-activity-getting-destroyed-after-calling-camera-intent – ADM Apr 19 '19 at 10:48
  • Yes I checked, nothing of Detach or Destroy is called. It looks like FragmentB just create FragmentA agin without destroy itself. I have no idea why it works like that. – Developer534255 Apr 19 '19 at 11:20
  • Also i didnt call the startActivityForResult from MainActivity, but from Fragment – Developer534255 Apr 19 '19 at 11:36
  • Still have no idea if someone will help i appreciate it – Developer534255 Apr 19 '19 at 12:38
  • Put logs into your activity.onCreate() – after the picture is taken, do you see this log message? Or maybe, FragmentA is loaded in activity.onResume()? – Alex Cohn Apr 20 '19 at 04:49
  • FragmentA is loaded when i back from camera intent, when i accept photo, but dont understand why FragmentB is none of ending lifecycle. Both of fragments extends regular Fragment. When I put debug to check whats happen, its look like in FragmentB execuded onActivityResult() and load previous fragment on himself. – Developer534255 Apr 20 '19 at 14:20

0 Answers0