2

The image is not returned when pic is taken from camera instead app closes and I see home screen of phone when pic is taken and I click on done from camera or click on cross of camera. I noticed onDestroy() method of fragment and fragment’s parent activity gets called as soon as camera is opened. I can see app in background tasks of phone.

android:configChanges="orientation|screenSize" I have tried above in manifest file following Android: Activity getting Destroyed after calling Camera Intent

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
 setUpPermissions() 
}

private fun setUpPermissions() {
    if (!hasPermissions(activity, *PERMISSIONS)) {
        ActivityCompat.requestPermissions(activity!!, PERMISSIONS, PERMISSION_ALL)
    } else {
      showDialogForCameraAndGallery()
    }
}

 private fun showDialogForCameraAndGallery() {
        val builder = android.support.v7.app.AlertDialog.Builder(activity!!)
        builder.setTitle("Select Option")
        builder.setItems(options, DialogInterface.OnClickListener { dialog, item ->
            if (options[item] == "Take Photo") {
                dialog.dismiss()
                openCamera()
            } else if (options[item] == "Choose From Gallery") {
                dialog.dismiss()
                openGallery()
            } else if (options[item] == "Cancel") {
                dialog.dismiss()
            }
        })
        builder.show()
    }

private fun openCamera() {
        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
     startActivityForResult(intent,
             CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
    }

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    Log.e(" on Activity called", "  frgamnet$requestCode")
}

override fun onDestroy() {
    Log.e(" ondestroy ", "of fragment");
    super.onDestroy()
}

Parent activity code

 @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e( " onActivity result ", "called");
    }

    @Override
    public void onDestroy() {
        Log.e(" desturo called", "activity");
        super.onDestroy();
    }
apurva
  • 117
  • 2
  • 12
  • Do you have "Don't keep activities" enabled in "Developer options" in Settings? – CommonsWare May 08 '19 at 13:30
  • @CommonsWare No – apurva May 08 '19 at 13:34
  • It is certainly possible that your app's process needs to be terminated when the camera app comes to the foreground. So the fact that your activity is destroyed is not a sign of a problem. It should not interfere with `onActivityResult()`, as Android should fork a fresh process for you. I suggest that you install a few camera apps and try each of them with your `ACTION_IMAGE_CAPTURE` `Intent`. Some of the apps will not support that action, but others will. Camera apps frequently have bugs, and so your current camera app may not be returning properly from `ACTION_IMAGE_CAPTURE`. – CommonsWare May 08 '19 at 13:48
  • The issue occurs when i call it from this fragment. If I call it from any activity, it works fine and onActivityResult is also called. Even in parent activity, this camera intent works fine.There is navigation drawer activity and fragments for drawer. one of the fragment's view intents to ABC activity which is parent activity for this fragment. Except this fragment, all code of app is in Java. – apurva May 08 '19 at 13:57
  • If you call `startActivityForResult()` from a fragment, `onActivityResult()` has to be implemented on that fragment. – CommonsWare May 08 '19 at 14:07
  • 1
    updated code. onActivityResult is not getting called anywhere. As soon as camera is opened, onDestroy in fragment and activity gets called.and when pic is taken, home screen of phone gets visible instead of application – apurva May 08 '19 at 14:12

0 Answers0