androidX中的活动构造器行为差异

2022-01-28 09:36:02 标签 androidandroid-fragmentsandroid-lifecycle

我只有一年的android开发经验。我认为android系统保证了生命周期的一致性回调。但几天前,我注意到一件奇怪的事。

我有一个片段附加到Activity类。Activity有一个空的构造函数。所以我们有文档中描述的一致行为。

MainActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Timber.d("TAG onCreate")
        setContentView(R.layout.activity_main)
    }
    override fun onStart() {
        super.onStart()
        Timber.d("TAG onStart")
    }
    override fun onResume() {
        super.onResume()
        Timber.d("TAG onResume")
    }
}

片段类

class PlayerFragment : Fragment() {
    private var _binding: FragmentPlayerBinding? = null
    private val binding: FragmentPlayerBinding get() = _binding!!
    override fun onAttach(context: Context) {
        super.onAttach(context)
        Timber.d("TAG onAttach")
        context.appComponent.inject(this)
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        Timber.d("TAG onCreateView")
        _binding = FragmentPlayerBinding.inflate(inflater, container, false)
        return binding.root
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Timber.d("TAG onViewCreated")
    }
    override fun onStart() {
        super.onStart()
        Timber.d("TAG onStart")
    }
    override fun onResume() {
        super.onResume()
        Timber.d("TAG onResume")
    }
}

如果我们运行应用程序,我们会在logcat中获得下一个日志

2021-09-05 14:21:00.162 D/MainActivity: TAG onCreate
2021-09-05 14:21:00.288 D/PlayerFragment: TAG onAttach
2021-09-05 14:21:00.293 D/PlayerFragment: TAG onCreateView
2021-09-05 14:21:00.481 D/PlayerFragment: TAG onViewCreated
2021-09-05 14:21:00.497 D/PlayerFragment: TAG onStart
2021-09-05 14:21:00.499 D/MainActivity: TAG onStart
2021-09-05 14:21:00.510 D/MainActivity: TAG onResume
2021-09-05 14:21:00.513 D/PlayerFragment: TAG onResume

然而,如果我们在Activity类中做一点改变,即移除setContentView方法,并将布局资源传递给Activity构造函数,我们可以看到一些神奇的行为

class MainActivity : AppCompatActivity(R.layout.activity_main) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Timber.d("TAG onCreate")
    }
    override fun onStart() {
        super.onStart()
        Timber.d("TAG onStart")
    }
    override fun onResume() {
        super.onResume()
        Timber.d("TAG onResume")
    }
}

检查logcat

2021-09-05 14:25:13.659 D/PlayerFragment: TAG onAttach
2021-09-05 14:25:13.666 D/PlayerFragment: TAG onCreateView
2021-09-05 14:25:13.997 D/PlayerFragment: TAG onViewCreated
2021-09-05 14:25:14.006 D/MainActivity: TAG onCreate
2021-09-05 14:25:14.133 D/PlayerFragment: TAG onStart
2021-09-05 14:25:14.136 D/MainActivity: TAG onStart
2021-09-05 14:25:14.182 D/MainActivity: TAG onResume
2021-09-05 14:25:14.185 D/PlayerFragment: TAG onResume

所以问题是为什么片段是附加到活动的onCreate方法的超调用活动?原因是什么?

注:https://medium。com/androiddevelopers/the-android-lifecycle-cheat-sheet-part-iii-fragments-afc87d4f37fd

阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码