Fixed
Status Update
Comments
il...@google.com <il...@google.com> #2
It seems that this ordering is caused by FragmentManager's addAddedFragments()
, which is being removed/reworked as part of
il...@google.com <il...@google.com> #3
Project: platform/frameworks/support
Branch: androidx-master-dev
commit 4ea7d82a7e17dd5a20aa0d581add0ba34874e485
Author: Ian Lake <ilake@google.com>
Date: Mon Jul 20 17:12:55 2020
Ensure fragments move to the expected state in op order
When using the new FragmentStateManager, we
should ensure that fragments move to their expected
state in the same order as the underlying operations -
i.e., in the order that they were added to the
FragmentTransaction.
This ensures that replace()'d fragments are stopped
before their replacement fragments move through
lifecycle methods, avoiding cases where both fragments
are simultaneously started.
Due to how the previous code works, this fix *only*
applies when using the new FragmentStateManager.
Test: new FragmentReorderingTest passes
BUG: 161654580
Change-Id: I32662cf3ec8bc5bd9a232b2572c91b5567a47f2f
M fragment/fragment/src/androidTest/java/androidx/fragment/app/FragmentReorderingTest.kt
M fragment/fragment/src/main/java/androidx/fragment/app/FragmentManager.java
https://android-review.googlesource.com/1367610
Branch: androidx-master-dev
commit 4ea7d82a7e17dd5a20aa0d581add0ba34874e485
Author: Ian Lake <ilake@google.com>
Date: Mon Jul 20 17:12:55 2020
Ensure fragments move to the expected state in op order
When using the new FragmentStateManager, we
should ensure that fragments move to their expected
state in the same order as the underlying operations -
i.e., in the order that they were added to the
FragmentTransaction.
This ensures that replace()'d fragments are stopped
before their replacement fragments move through
lifecycle methods, avoiding cases where both fragments
are simultaneously started.
Due to how the previous code works, this fix *only*
applies when using the new FragmentStateManager.
Test: new FragmentReorderingTest passes
BUG: 161654580
Change-Id: I32662cf3ec8bc5bd9a232b2572c91b5567a47f2f
M fragment/fragment/src/androidTest/java/androidx/fragment/app/FragmentReorderingTest.kt
M fragment/fragment/src/main/java/androidx/fragment/app/FragmentManager.java
Description
androidx.appcompat:appcompat:1.1.0
androidx.fragment:fragment:1.3.0-alpha03
Theme used: Theme.AppCompat.Light.DarkActionBar
Devices/Android versions reproduced on: Pixel 4 (RP1A.200115.001.C1)
Recreating this issue per comment on
Minimal code to reproduce is below. Clicking on the displayed button will cause a crash, as the prior expectation was that in onAttachFragment(), the FragmentManager will contain the Fragment that is passed as a parameter to onAttachFragment(), but this is no longer the case.
_______________
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
private const val FRAGMENT_TAG = "tag"
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState == null) {
supportFragmentManager
.beginTransaction()
.add(android.R.id.content, StartingFragment(), FRAGMENT_TAG)
.commit()
}
}
override fun onAttachFragment(fragment: Fragment) {
super.onAttachFragment(fragment)
val fragmentByTag = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG)
if (fragment != fragmentByTag) {
throw IllegalStateException("Expected $fragment but found $fragmentByTag")
}
}
fun replaceFragment() {
supportFragmentManager
.beginTransaction()
.replace(android.R.id.content, ReplacementFragment(), FRAGMENT_TAG)
.addToBackStack(null)
.commit()
}
class StartingFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return Button(requireContext()).apply {
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
text = "Replace fragment"
setOnClickListener {
(requireActivity() as MainActivity).replaceFragment()
}
}
}
}
class ReplacementFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return ImageView(requireContext()).apply {
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
setBackgroundColor(Color.BLUE)
}
}
}
}