title: Different behaviour on returning from startActivityForResult() among Android 10 and 11
body: <p>I have a strange behaviour when I run my app in Android 11 and 10. I have an activity containing a searchview, a spinner and a recycler view to filter and show a list of persons, and a fragment that displays the details of the selected one.</p>
  <p>In the onCreate() method of the activity I set the listener of the search view to filter patient list everytime the latter is modified</p>
  <pre><code>mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
  
              @Override
              public boolean onQueryTextSubmit(String query) {
                  return false;
              }
  
              @Override
              public boolean onQueryTextChange(String query) {
  
                  easyLog.error(&quot;onQueryTextChange&quot;);
  
                  if(mCurrentPosition != -1)
                      mPatientListAdapter.setSelection(mCurrentPosition, false);
  
                  easyLog.error(&quot;spinner position --&gt; &quot; + mDomainSpinner.getSelectedItemPosition());
                  if(mDomainSpinner.getSelectedItemPosition() != -1) {
  
                      if (mDomainSpinner.getSelectedItemPosition() != 0) {
  
                          easyLog.error(&quot;spinner position != 0&quot;);
                          mPatientListAdapter.getFilter().filter(mDomainSpinner.getSelectedItem().toString() + &quot;|&quot; + query.trim());
                      } else {
                          easyLog.error(&quot;spinner position == 0&quot;);
                          mPatientListAdapter.getFilter().filter(&quot;|&quot; + query.trim());
                      }
                  }
  
                  mCurrentPosition = -1;
  
                  DetailsFragment removeDetailsFragment = (DetailsFragment) getSupportFragmentManager().findFragmentById(R.id.details);
                  if(removeDetailsFragment != null)
                      getSupportFragmentManager().beginTransaction().remove(removeDetailsFragment).commit();
  
                  MenuItem start = mToolbar.getMenu().findItem(R.id.nav_start);
                  MenuItem sync = mToolbar.getMenu().findItem(R.id.nav_sync);
  
                  start.setVisible(false);
                  sync.setVisible(false);
  
                  return true;
              }
          });
  </code></pre>
  <p>In the fragment the pressure of a button let the user to start the camera and save a photo</p>
  <pre><code>// Open camera to let user to take a photo of patient data handling consent
          mPatientDataHandlingUploadButton.setOnClickListener(view -&gt; {
  
              // Create image file name
              String imageFileName = &quot;dataHandling_patientId_&quot; + getPatientCode() + &quot;.jpeg&quot;;
              File storageDir = mActivity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
              File image = new File(storageDir, imageFileName);
              Intent takePictureIntent  = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
              Uri photoURI = FileProvider.getUriForFile(mActivity, mActivity.getPackageName() + &quot;.provider&quot;, image);
              takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
              if(getActivity() != null)
                  getActivity().startActivityForResult(takePictureIntent, GlobalConfiguration.getRequestCodeImage());
          });
  </code></pre>
  <p>The result of the startActivityForResult is handled in the activity</p>
  <pre><code>// Managing the return from camera after taking the patient data handling consent
      @RequiresApi(api = Build.VERSION_CODES.M)
      @SuppressWarnings({&quot;ConstantConditions&quot;, &quot;ResultOfMethodCallIgnored&quot;})
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          easyLog.error(&quot;onActivityResult&quot;);
          if (requestCode == GlobalConfiguration.getRequestCodeImage() &amp;&amp; resultCode == RESULT_OK) {
  
              FileInputStream fis;
              File file = new File(mActivity.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
                                                                 &quot;/&quot; + &quot;dataHandling_patientId_&quot; + getPatientCode() + &quot;.jpeg&quot;);
              int size = (int) file.length();
              byte[] array = new byte[size];
  
              try {
  
                  // Converting photo to byte array
                  fis = new FileInputStream(file);
                  BufferedInputStream bis = new BufferedInputStream(fis);
                  bis.read(array, 0, array.length);
                  bis.close();
  
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
              file.delete();
  
              // Saving patient attachment to DB with photo data as byte array
              PatientAttachment patientAttachment = new PatientAttachment(getPatientCode(),
                      InternalCode.CODE_ID,
                      GlobalConfiguration.PatientAttachmentType.data_handling,
                      array,
                      System.currentTimeMillis(),
                      false);
  
              Button patientDataHandlingUploadButton = mActivity.findViewById(R.id.patient_data_handling_upload_button);
              Button patientDataHandlingAlertButton = mActivity.findViewById(R.id.patient_data_handling_alert_button);
              TextView patientDataHandlingStatusTextView = mActivity.findViewById(R.id.patient_data_handling_status);
  
              mCompositeDisposable.add(
                      Completable.fromAction(() -&gt; mInternalDatabaseInstance.getInternalDatabase().patientAttachmentDao().insert(patientAttachment))
                              .subscribeOn(Schedulers.io())
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribe(() -&gt; {
                                      easyLog.info(&quot;[onActivityResult] Data handling consent stored correctly. Sending to to remote server...&quot;);
                                      patientDataHandlingUploadButton.setVisibility(View.GONE);
                                      patientDataHandlingStatusTextView.setVisibility(View.VISIBLE);
                                          patientDataHandlingStatusTextView.setTextColor(Color.BLACK);
                                      patientDataHandlingStatusTextView.setText(R.string.sending);
                                      PatientAttachmentUploadRequest patientAttachmentUploadRequest =
                                              PatientAttachmentMapper.mapToPatientAttachmentUploadRequest(patientAttachment);
                                      mCompositeDisposable.add(
                                              mNetworkInstance.sendDataHandlingConsent(
                                                      GlobalConfiguration.HealthCenter.EASYCARE,
                                                      mApplication.getUser().getUsername(),
                                                      mApplication.getUser().getPassword(),
                                                      patientAttachmentUploadRequest)
                                                          .subscribeOn(Schedulers.io())
                                                          .observeOn(AndroidSchedulers.mainThread())
                                                          .subscribe( () -&gt; {
                                                                      easyLog.info(&quot;[onActivityResult] Data handling consent for patient with code &quot; + patientAttachmentUploadRequest.getPatient().getCode() + &quot; uploaded successfully!&quot;);
                                                                      patientDataHandlingStatusTextView.setText(R.string.button_ok);
                                                                      patientDataHandlingStatusTextView.setTextColor(mActivity.getColor(R.color.green));
                                                                      patientAttachment.setData(null);
                                                                      patientAttachment.setTimestamp(null);
                                                                      patientAttachment.setDataSent(true);
                                                                      mCompositeDisposable.add(
                                                                              Completable.fromAction(() -&gt; mInternalDatabaseInstance.getInternalDatabase().patientAttachmentDao().upsert(patientAttachment))
                                                                                      .observeOn(AndroidSchedulers.mainThread())
                                                                                      .subscribeOn(Schedulers.io())
                                                                                      .subscribe( () -&gt; easyLog.info(&quot;[onActivityResult] Patient attachment for patient with code &quot; + patientAttachment.getPatientCode() + &quot; updated correctly&quot;),
                                                                                           throwable -&gt; easyLog.error(&quot;[onActivityResult] Error during update of patient attachment for patient with code &quot; + patientAttachment.getPatientCode())
                                                                                      )
                                                                      );
                                                                  },
                                                                  throwable -&gt; {
                                                                      easyLog.error(&quot;[onActivityResult] Error uploading data handling consent for patient with code &quot; + patientAttachmentUploadRequest.getPatient().getCode());
                                                                      patientDataHandlingStatusTextView.setVisibility(View.GONE);
                                                                      patientDataHandlingAlertButton.setVisibility(View.VISIBLE);
                                                                  }
                                                          )
                                      );
                              },
                              throwable -&gt; easyLog.error(&quot;[onActivityResult] Error during data handling consent storing&quot;)
                      )
              );
          }
      }
  
  </code></pre>
  <p>Here comes the problem: in Android 11 I press the button, the camera opens, I take the photo and the app return to the activity saving the photo without problem.
  The exact same situation in Android 10 causes the app crash while returning from the camera activity</p>
  <pre><code>2021-12-27 16:09:20.340 23425-23425/it.*********.easycareprofessional.debug E/AndroidRuntime: FATAL EXCEPTION: main
      Process: it.*********.easycareprofessional.debug, PID: 23425
      java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.Filter it.*********.easycareprofessional.activities.examination.search.PatientListAdapter.getFilter()' on a null object reference
          at it.*********.easycareprofessional.activities.examination.search.PatientSelectionActivity$2.onQueryTextChange(PatientSelectionActivity.java:297)
          at android.widget.SearchView.onTextChanged(SearchView.java:1521)
          at android.widget.SearchView.access$2100(SearchView.java:113)
          at android.widget.SearchView$10.onTextChanged(SearchView.java:2154)
          at android.widget.TextView.sendOnTextChanged(TextView.java:11621)
          at android.widget.TextView.setText(TextView.java:6833)
          at android.widget.TextView.setText(TextView.java:6630)
          at android.widget.EditText.setText(EditText.java:140)
          at android.widget.TextView.setText(TextView.java:6582)
          at android.widget.TextView.onRestoreInstanceState(TextView.java:6449)
          at android.view.View.dispatchRestoreInstanceState(View.java:21577)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.View.restoreHierarchyState(View.java:21555)
          at com.android.internal.policy.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2261)
          at android.app.Activity.onRestoreInstanceState(Activity.java:1593)
          at android.app.Activity.performRestoreInstanceState(Activity.java:1548)
          at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1354)
          at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3662)
          at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
          at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
          at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
          at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2261)
          at android.os.Handler.dispatchMessage(Handler.java:107)
          at android.os.Looper.loop(Looper.java:237)
          at android.app.ActivityThread.main(ActivityThread.java:8107)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
  2021-12-27 16:09:20.341 23425-23425/it.*********.easycareprofessional.debug E/it.*********.easycareprofessional.application.App: [handleUncaughtException] Thread = main (2)
  2021-12-27 16:09:20.343 23425-23425/it.*********.easycareprofessional.debug E/it.*********.easycareprofessional.application.App: [handleUncaughtException] Throwable message --&gt; Attempt to invoke virtual method 'android.widget.Filter it.*********.easycareprofessional.activities.examination.search.PatientListAdapter.getFilter()' on a null object reference
  2021-12-27 16:09:20.347 23425-23425/it.*********.easycareprofessional.debug E/it.*********.easycareprofessional.application.App: [handleUncaughtException] Stacktrace --&gt; java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.Filter it.*********.easycareprofessional.activities.examination.search.PatientListAdapter.getFilter()' on a null object reference
          at it.*********.easycareprofessional.activities.examination.search.PatientSelectionActivity$2.onQueryTextChange(PatientSelectionActivity.java:297)
          at android.widget.SearchView.onTextChanged(SearchView.java:1521)
          at android.widget.SearchView.access$2100(SearchView.java:113)
          at android.widget.SearchView$10.onTextChanged(SearchView.java:2154)
          at android.widget.TextView.sendOnTextChanged(TextView.java:11621)
          at android.widget.TextView.setText(TextView.java:6833)
          at android.widget.TextView.setText(TextView.java:6630)
          at android.widget.EditText.setText(EditText.java:140)
          at android.widget.TextView.setText(TextView.java:6582)
          at android.widget.TextView.onRestoreInstanceState(TextView.java:6449)
          at android.view.View.dispatchRestoreInstanceState(View.java:21577)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4699)
          at android.view.View.restoreHierarchyState(View.java:21555)
          at com.android.internal.policy.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2261)
          at android.app.Activity.onRestoreInstanceState(Activity.java:1593)
          at android.app.Activity.performRestoreInstanceState(Activity.java:1548)
          at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1354)
          at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3662)
          at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
          at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
          at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
          at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2261)
          at android.os.Handler.dispatchMessage(Handler.java:107)
          at android.os.Looper.loop(Looper.java:237)
          at android.app.ActivityThread.main(ActivityThread.java:8107)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
  </code></pre>
  <p>Seems that on Android 10 while returning from the camera the focus goes the searchview like I was typing something in the searchview, and meanwhile all the views that I defined before are now null.</p>
  <p>Am I missing something?! Why so different behaviours between those two version of Android!?</p>
  <p>Thanks for the help!</p>