Activity代码
val fragments = arrayListOf(
CustomFragment.get(1),
CustomFragment.get(2),
CustomFragment.get(3),
CustomFragment.get(4),
)
// FragmentStatePagerAdapter(supportFragmentManager,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
findViewById(R.id.viewpage2).adapter =
object : FragmentStateAdapter(supportFragmentManager, lifecycle) {
override fun getItemCount(): Int {
return fragments.size
}
override fun createFragment(position: Int): Fragment {
return fragments[position]
}
}
Fragment代码
class CustomFragment : Fragment() {
companion object{
fun get(index:Int): CustomFragment {
return CustomFragment().apply {
arguments = Bundle().apply {
putInt("index",index)
}
}
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
log("onCreateView")
return inflater.inflate(R.layout.fragment_custom, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val nextInt = Random.nextInt(0, 5)
val colors = arrayListOf(
Color.RED,
Color.BLACK,
Color.BLUE,
Color.DKGRAY,
Color.WHITE
)
// Color.RED
// Color.BLACK
// Color.BLUE
// Color.DKGRAY
// Color.WHITE
// Color.LTGRAY
// Color.GREEN
view.setBackgroundColor(colors[nextInt])
log("onViewCreated")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
log("onCreate")
}
override fun onStart() {
super.onStart()
log("onStart")
}
override fun onResume() {
super.onResume()
log("onResume")
}
override fun onPause() {
super.onPause()
log("onPause")
}
override fun onStop() {
super.onStop()
log("onStop")
}
override fun onDestroyView() {
super.onDestroyView()
log("onDestroyView")
}
override fun onDestroy() {
super.onDestroy()
log("onDestroy")
}
fun log(tag:String){
val index = arguments?.getInt("index", 0)
Log.d("CustomFragment", "log: CustomFragment$index 的生命周期: $tag")
}
}
- 刚进入页面调用情况
CustomFragment: log: CustomFragment1 的生命周期: onCreate
CustomFragment: log: CustomFragment1 的生命周期: onCreateView
CustomFragment: log: CustomFragment1 的生命周期: onViewCreated
CustomFragment: log: CustomFragment1 的生命周期: onStart
CustomFragment: log: CustomFragment1 的生命周期: onResume
- 滑动到index=2时调用情况
CustomFragment: log: CustomFragment2 的生命周期: onCreate
CustomFragment: log: CustomFragment2 的生命周期: onCreateView
CustomFragment: log: CustomFragment2 的生命周期: onViewCreated
CustomFragment: log: CustomFragment2 的生命周期: onStart
CustomFragment: log: CustomFragment1 的生命周期: onPause
CustomFragment: log: CustomFragment2 的生命周期: onResume
- 滑动到index=3时调用情况
CustomFragment: log: CustomFragment3 的生命周期: onCreate
CustomFragment: log: CustomFragment3 的生命周期: onCreateView
CustomFragment: log: CustomFragment3 的生命周期: onViewCreated
CustomFragment: log: CustomFragment3 的生命周期: onStart
CustomFragment: log: CustomFragment2 的生命周期: onPause
CustomFragment: log: CustomFragment3 的生命周期: onResume
- 滑动到index=4时调用情况
CustomFragment: log: CustomFragment4 的生命周期: onCreate
CustomFragment: log: CustomFragment4 的生命周期: onCreateView
CustomFragment: log: CustomFragment4 的生命周期: onViewCreated
CustomFragment: log: CustomFragment4 的生命周期: onStart
CustomFragment: log: CustomFragment3 的生命周期: onPause
CustomFragment: log: CustomFragment4 的生命周期: onResume
- 熄屏
CustomFragment: log: CustomFragment4 的生命周期: onPause
CustomFragment: log: CustomFragment1 的生命周期: onStop
CustomFragment: log: CustomFragment2 的生命周期: onStop
CustomFragment: log: CustomFragment3 的生命周期: onStop
CustomFragment: log: CustomFragment4 的生命周期: onStop
- 亮屏
CustomFragment: log: CustomFragment1 的生命周期: onStart
CustomFragment: log: CustomFragment2 的生命周期: onStart
CustomFragment: log: CustomFragment3 的生命周期: onStart
CustomFragment: log: CustomFragment4 的生命周期: onStart
CustomFragment: log: CustomFragment4 的生命周期: onResume