title: Can not perform this action after onSaveInstanceState when closing an activity
body: <p>I know this error is very common, but I couldn't find a solution to my problem in the way too many places this error is brought up.</p>
  <p>I'm developing an app in order to store and sort TV shows. I've got a main activity with some fragments, with a <code>HomeFragment</code> which is the home page, with an 'Add show' button, and below a <code>recyclerView</code> with all my shows.</p>
  <p>When clicking on the 'Add show' button, I start a new activity in order to fill a form and then create the show with the provided informations. No problem here, that works as it should. Now I'm trying to add the possibility to edit the shows by clicking on them in the <code>recyclerView</code> I talked about above. This also brings up the same activity as the 'Add show' button, but this time with the show informations.</p>
  <p>And this is from this page that the problem seems to be coming. In the form activity, I have an button in which I pick an image for the show. When editing the show, if I change the image, I get no error, but if I change something else, for example the name, without changing this image, when clicking the confirm button, the show is correctly edited but the app crashes with the <code>java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState</code> error.</p>
  <p>The error seems to be coming from the fragment part, with the transaction being unable to commit (I've searched for a while so I began to understand why that wasn't working, but couldn't determine which part of the code makes it this way). Here is the fragment:</p>
  <pre><code>class HomeFragment(private val context: MainActivity) : Fragment() {
  
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
          val view = inflater.inflate(R.layout.fragment_home, container, false)
  
          view.findViewById&lt;Button&gt;(R.id.button_add_show).setOnClickListener{
              startActivity(Intent(context, AddShowActivity::class.java))
          }
          
          val verticalRecyclerView = view.findViewById&lt;RecyclerView&gt;(R.id.vertical_recycler_view)
          verticalRecyclerView.adapter = ShowAdapter(context, showList, R.layout.item_show)
  
          return view
      }
  }
  </code></pre>
  <p>And here the <code>MainActivity</code> part where it's loaded:</p>
  <pre><code>private fun loadFragment(fragment: Fragment){
     // Load repository
     val repo = ShowRepository()
  
      // Update shows list
      repo.updateData{
          // Inject fragment into fragment_container
          val transaction = supportFragmentManager.beginTransaction()
          transaction.replace(R.id.fragment_container, fragment)
          transaction.addToBackStack(null)
          transaction.commit()
      }
  }
  </code></pre>
  <p>Here is the code of my <code>AddShowActivity</code>, which renders the form to fill:</p>
  <pre><code>class AddShowActivity : AppCompatActivity() {
      private var fileImage: Uri? = null
      private lateinit var uploadedImage: ImageView
      private lateinit var editTextName: EditText
      private lateinit var editTextNote: EditText
      private lateinit var editTextDescription: EditText
      private lateinit var editTextReview: EditText
      private lateinit var datePicker: DatePicker
      private var currentShow: ShowModel? = null
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_add_show)
          setupComponents()
          setupButtons()
          
          // Get show when editing
          if(intent.extras != null){
              val position = intent.extras!!.getInt(&quot;position&quot;)
              currentShow = showList[position]
          }
  
          initializeComponents()
  
      }
  
      private fun setupComponents() {
          editTextName = findViewById(R.id.name_input)
          editTextNote = findViewById(R.id.note_input)
          editTextDescription = findViewById(R.id.description_input)
          editTextReview = findViewById(R.id.review_input)
          uploadedImage = findViewById(R.id.preview_image)
          datePicker = findViewById(R.id.watch_date_input)
      }
  
      private fun setupButtons(){
          val pickupImageButton = findViewById&lt;Button&gt;(R.id.upload_image_button)
          pickupImageButton.setOnClickListener{
              pickupImage()
          }
  
          val confirmButton = findViewById&lt;Button&gt;(R.id.confirm_button)
          confirmButton.setOnClickListener{
              sendForm()
              val toastText = when(currentShow){
                  null -&gt; &quot;Show added&quot;
                  else -&gt; &quot;Show edited&quot;
              }
              Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show()
              this.finish()
          }
      }
  
      @SuppressLint(&quot;NewApi&quot;)
      private fun initializeComponents() {
          if(currentShow != null){
              editTextName.setText(currentShow!!.name)
              editTextNote.setText(currentShow!!.note.toString())
              editTextDescription.setText(currentShow!!.description)
              editTextReview.setText(currentShow!!.review)
              Glide.with(this).load(Uri.parse(currentShow!!.imageUrl)).into(uploadedImage)
          }
      }
  
      private fun sendForm(){
          val repo = ShowRepository()
          if(fileImage == null)createShow(repo)
          else{
              if(currentShow != null)repo.deleteImage(currentShow!!)
              repo.uploadImage(fileImage!!){
                  createShow(repo)
              }
          }
      }
  
      private fun createShow(repo: ShowRepository){
          val showName = editTextName.text.toString()
          val showNote = parseInt(editTextNote.text.toString())
          val description = editTextDescription.text.toString()
          val review = editTextReview.text.toString()
          val showWatchDate = getWatchDate(datePicker)
          val downloadImageUrl = downloadImageUri.toString()
  
          val show = ShowModel(UUID.randomUUID().toString(), showName, showWatchDate, showNote, downloadImageUrl, description, review)
          if(currentShow != null){
              show.id = currentShow!!.id
              repo.updateShow(show)
          }
          else repo.insertShow(show)
      }
  
      private fun getWatchDate(datePicker: DatePicker): String {
          var day = datePicker.dayOfMonth.toString()
          if(day.toInt() &lt; 10)day = &quot;0$day&quot;
          var month = (datePicker.month + 1).toString()
          if(month.toInt() &lt; 10)month = &quot;0$month&quot;
          val year = datePicker.year.toString()
          return &quot;$day-$month-$year&quot;
      }
  
      private fun pickupImage(){
          val intent = Intent()
          intent.type = &quot;image/&quot;
          intent.action = Intent.ACTION_GET_CONTENT
          startActivityForResult(Intent.createChooser(intent, &quot;Select Picture&quot;), 47)
      }
  
      override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
          super.onActivityResult(requestCode, resultCode, data)
          if(resultCode == Activity.RESULT_OK &amp;&amp; requestCode == 47){
              if(data == null || data.data == null)return
              fileImage = data.data
              uploadedImage.setImageURI(fileImage)
          }
      }
  }
  </code></pre>
  <p>Here the <code>ShowRepository</code>, which handles the communication with the Firebase database:</p>
  <pre><code>class ShowRepository {
  
      object Singleton{
          // Link to bucket
          private val BUCKET_URL: String = &quot;gs://tv-memories.appspot.com&quot;
  
          // Storage connexion
          val storageReference = FirebaseStorage.getInstance().getReferenceFromUrl(BUCKET_URL)
  
          // Database connexion
          val databaseRef = FirebaseDatabase.getInstance().getReference(&quot;shows&quot;)
  
          // List containing all shows
          val showList = arrayListOf&lt;ShowModel&gt;()
  
          // Contains current image link
          var downloadImageUri: Uri? = null
      }
  
      fun updateData(callback: () -&gt; Unit){
          // Absorb data from databaseRef
          databaseRef.addValueEventListener(object : ValueEventListener {
              override fun onDataChange(snapshot: DataSnapshot) {
                  // Remove old shows
                  showList.clear()
  
                  // Get list
                  for(ds in snapshot.children){
                      //Build show object
                      val show = ds.getValue(ShowModel::class.java)
  
                      // Verify show isn't null
                      if(show != null){
                          // Add show to the list
                          showList.add(show)
                      }
                  }
                  // Activate callback
                  callback()
              }
  
              override fun onCancelled(p0: DatabaseError) {
  
              }
  
          })
      }
  
      // Upload files on storage
      fun uploadImage(file: Uri, callback: () -&gt; Unit){
          val fileName = UUID.randomUUID().toString() + &quot;.jpg&quot;
          val ref = storageReference.child(fileName)
          val uploadTask = ref.putFile(file)
  
          uploadTask.continueWithTask(Continuation&lt;UploadTask.TaskSnapshot, Task&lt;Uri&gt;&gt;{ task -&gt;
              if(!task.isSuccessful){
                  task.exception?.let{throw it}
              }
  
              return@Continuation ref.downloadUrl
          }).addOnCompleteListener{ task -&gt;
              if(task.isSuccessful){
                  downloadImageUri = task.result
                  callback()
              }
          }
      }
  
      fun deleteImage(show: ShowModel){
          val photoRef: StorageReference = FirebaseStorage.getInstance().getReferenceFromUrl(show.imageUrl)
          photoRef.delete()
      }
  
      fun updateShow(show: ShowModel) = databaseRef.child(show.id).setValue(show)
  
      fun insertShow(show: ShowModel) = databaseRef.child(show.id).setValue(show)
  
      fun deleteShow(show: ShowModel){
          databaseRef.child(show.id).removeValue()
          deleteImage(show)
      }
  }
  </code></pre>
  <p>And the full traceback of the error:</p>
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: fr.steph.showmemories, PID: 18296
      java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
          at androidx.fragment.app.FragmentManager.checkStateLoss(FragmentManager.java:1844)
          at androidx.fragment.app.FragmentManager.enqueueAction(FragmentManager.java:1884)
          at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:329)
          at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:294)
          at fr.steph.showmemories.MainActivity$loadFragment$1.invoke(MainActivity.kt:49)
          at fr.steph.showmemories.MainActivity$loadFragment$1.invoke(MainActivity.kt:44)
          at fr.steph.showmemories.ShowRepository$updateData$1.onDataChange(ShowRepository.kt:61)
          at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
          at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
          at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
          at android.os.Handler.handleCallback(Handler.java:873)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:214)
          at android.app.ActivityThread.main(ActivityThread.java:7078)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
  </code></pre>