美文网首页实用
Android 实现单Activity多Fragment页面项

Android 实现单Activity多Fragment页面项

作者: Gxinyu | 来源:发表于2022-05-08 17:16 被阅读0次

    前言

      以往项目都是多Activity实现,总体实现很方便。但是现在流行使用单Activity+多Fragment的形式实现项目,例如比较流行的Navigation基本可以满足,但是有时候会存在从一个页面跳转到另一个页面并且回传数据的问题,Navigation组件的话内部实现原理是使用replace()方式添加Fragment,这样会导致两个问题,第一个是每次切换Fragment都会重建,第二是无法做到数据的回传,网上也有一些针对此问题的解决方案。本文并不打算使用Navigation实现,而是通过使用Results API的方式。

    Fragment通信

      今年 Google 推出了 Fragment Result API 和 Activity Results API,用来取代之前的 Activity 和 Fragment 之间通信方式的不足。Fragment Result API 主要介绍 Fragment 间通信的新方式,是在 Fragment 1.3.0-alpha04 新增加的 API ,而现在最新版本已经到 fragment-1.3.0-beta01 应该很快就能应用在项目里面了。本文就是通过Result API实现Fragment之间的通信。

    接受数据

    在需要接受数据的Fragment里面注册setFragmentResultListener()监听

    FragmentManager.setFragmentResultListener(
        requestKey,
        lifecycleOwner,
        FragmentResultListener { requestKey: String, result: Bundle ->
            // Handle result
        })
    

    参数解释:
    requestKey :请求跳转到另一个fragment的字符串key
    lifecycleOwner:生命周期观察者
    FragmentResultListener :fragment结果回到监听

    发送数据

    当前fragment想把数据传递给上一个页面,则需要通过设置setFragmentResult()方法设置数据,

    parentFragmentManager.setFragmentResult(
        requestKey, // Same request key FragmentA used to register its listener
        bundleOf(key to value) // The data to be passed to FragmentA
    )
    

    参数解释:
    requestKey :请求跳转到另一个fragment的字符串key
    Bundle:用于传递回调结果的bundle

    Fragment携带数据跳转

      如果此次Fragment是做携带数据的跳转则必须携带requestKey,requestKey与设置回调数据时候使用同一个requestKey,因为想把回调封装进跳转的参数中,因此resultCallback充当Fragment回到结果的监听回调。

     /**
         * fragment中添加fragment(replace方式)
         * @param target Fragment 目标fragment
         * @param requestKey String 请求key
         * @param args Bundle 携带参数
         * @param resultCallback FragmentResultListener 跳转结果回调
         */
        fun startForResult(target: Fragment, requestKey: String, args: Bundle? = null, resultCallback: FragmentResultListener) {
            val targetArgs: Bundle = args ?: Bundle()
            targetArgs.putString(REQUEST_KEY, requestKey)
            target.arguments = targetArgs
            manager.beginTransaction()
                .setReorderingAllowed(true)
                .add(R.id.fragment_root, target)
                .addToBackStack(tag ?: target.javaClass.simpleName)
                .commit()
            target.setFragmentResultListener(requestKey) { it, bundle ->
                resultCallback.onFragmentResult(it, bundle)
            }
        }
    

    BaseFragment封装

      关于ViewBinding和ViewModel的封装在前一篇文章已经讲了。对于ViewModel的封装当初公司有人提议是每个Fragment必须去实现一个ViewModel,如果没有则使用通用的EmptyViewModel代替,这样考虑是为了不让代码中出现太多?空校验,这一块看自己决定。整个BaseFragment代码如下:

    abstract class BaseFragment<ViewModelLazy : ViewModel, Binding : ViewBinding> : Fragment(), View.OnClickListener {
    
        protected lateinit var TAG: String
        protected lateinit var ctx: AppCompatActivity
        protected lateinit var binding: Binding
        protected lateinit var manager: FragmentManager
        protected var requestKey: String? = null
        protected var viewModel: ViewModelLazy? = null
    
        companion object {
            const val REQUEST_KEY = "requestKey"
        }
    
        override fun onAttach(context: Context) {
            super.onAttach(context)
            this.ctx = context as AppCompatActivity
            TAG = this::class.java.simpleName
            manager = ctx.supportFragmentManager
        }
    
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?,
        ): View? {
            Log.e(TAG, "fragment_onCreateView")
            viewModel = bindViewModel()
            arguments?.let { bindBundle(it) }
            binding = bindLayout()
            initView()
            initData()
            bindObserver()
            bindListener()
            return binding.root
        }
    
        /**
         * 绑定ViewModel
         * @return ViewModelLazy
         */
        open fun bindViewModel(): ViewModelLazy? = null
    
        /**
         * 绑定intent
         * @param bundle Bundle
         */
        open fun bindBundle(bundle: Bundle) {
            requestKey = bundle.getString(REQUEST_KEY)
        }
    
        /**
         * 绑定布局
         * @return View
         */
        abstract fun bindLayout(): Binding
    
        /**
         * 初始化view
         */
        open fun initView() {
    
        }
    
        /**
         * 初始化数据
         */
        open fun initData() {
    
        }
    
        /**
         * 重新加载
         */
        open fun onReload() {
    
        }
    
        /**
         * 绑定observer
         */
        open fun bindObserver() {
    
        }
    
        /**
         * 绑定监听
         */
        open fun bindListener() {
    
        }
    
        /**
         * 监听回调
         * @param v View
         */
        override fun onClick(v: View?) {
    
        }
    
        /**
         * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         * +++++++++++++++++++++++++++++++++++++++++++++++++fragment回传数据++++++++++++++++++++++++++++++++
         * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         */
        /**
         * 设置回传fragment数据
         * @param args Bundle
         */
        fun onBackResult(args: Bundle) {
            requestKey?.let {
                setFragmentResult(it, args)
                manager.popBackStack()
            }
        }
    
        interface FragmentResultListener {
            /**
             * Callback used to handle results passed between fragments.
             *
             * @param requestKey key used to store the result
             * @param result result passed to the callback
             */
            fun onFragmentResult(requestKey: String, result: Bundle)
        }
    
    
        /**
         * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         * +++++++++++++++++++++++++++++++++++++++++++++++++页面跳转++++++++++++++++++++++++++++++++++++++++
         * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         */
        /**
         * fragment中添加fragment(add方式)
         * @param target Fragment
         * @param args Bundle
         */
        fun start(target: Fragment, args: Bundle? = null) {
            args?.let {
                target.arguments = it
            }
            manager.beginTransaction()
                .setReorderingAllowed(true)
                .add(R.id.fragment_root, target)
                .addToBackStack(target.javaClass.simpleName)
                .commit()
        }
    
        /**
         * fragment中添加fragment(add方式)
         * @param target Fragment
         * @param args Bundle
         */
        fun start(target: String, args: Bundle? = null) {
            val navigation = ARouter.getInstance().build(target).navigation() as BaseFragment<*, *>
            args?.let {
                navigation.arguments = it
            }
            manager.beginTransaction()
                .setReorderingAllowed(true)
                .add(R.id.fragment_root, navigation)
                .addToBackStack(target.javaClass.simpleName)
                .commit()
        }
    
        /**
         * fragment中添加fragment(replace方式)
         * @param target Fragment
         * @param args Bundle
         */
        fun replace(target: Fragment, args: Bundle? = null) {
            args?.let {
                target.arguments = it
            }
            manager.beginTransaction()
                .setReorderingAllowed(true)
                .replace(R.id.fragment_root, target)
                .addToBackStack(target.javaClass.simpleName)
                .commit()
        }
    
        /**
         * fragment中添加fragment(replace方式)
         * @param target Fragment
         * @param args Bundle
         */
        fun startRoot(target: Fragment, args: Bundle? = null) {
            args?.let {
                target.arguments = it
            }
            clearFragmentStack()
            manager.beginTransaction()
                .setReorderingAllowed(true)
                .replace(R.id.fragment_root, target)
                .addToBackStack(target.javaClass.simpleName)
                .commit()
        }
    
        /**
         * fragment中添加fragment(replace方式)
         * @param target Fragment 目标fragment
         * @param requestKey String 请求key
         * @param args Bundle 携带参数
         * @param resultCallback FragmentResultListener 跳转结果回调
         */
        fun startForResult(target: Fragment, requestKey: String, args: Bundle? = null, resultCallback: FragmentResultListener) {
            val targetArgs: Bundle = args ?: Bundle()
            targetArgs.putString(REQUEST_KEY, requestKey)
            target.arguments = targetArgs
            manager.beginTransaction()
                .setReorderingAllowed(true)
                .add(R.id.fragment_root, target)
                .addToBackStack(tag ?: target.javaClass.simpleName)
                .commit()
            target.setFragmentResultListener(requestKey) { it, bundle ->
                resultCallback.onFragmentResult(it, bundle)
            }
        }
    
        /**
         * 清空当前栈所有fragment
         */
        private fun clearFragmentStack() {
            val count: Int = manager.backStackEntryCount
            if (count > 0) {
                val entry: FragmentManager.BackStackEntry =
                    manager.getBackStackEntryAt(0)
                manager.popBackStack(
                    entry.name,
                    FragmentManager.POP_BACK_STACK_INCLUSIVE
                )
            }
        }
    
        /**
         * 退出当前的fragment
         */
        fun onBackPressed() {
            manager.popBackStack()
        }
    
        /**
         * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         * +++++++++++++++++++++++++++++++++++++++++++++++++生命周期++++++++++++++++++++++++++++++++++++++++
         * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         */
        override fun onResume() {
            super.onResume()
            Log.d(TAG, "fragment_onResume")
        }
    
        override fun onPause() {
            super.onPause()
            Log.d(TAG, "fragment_onPause")
        }
    
        override fun onStart() {
            super.onStart()
            Log.d(TAG, "fragment_onStart")
        }
    
        override fun onStop() {
            super.onStop()
            Log.d(TAG, "fragment_onStop")
        }
    
        override fun onDestroy() {
            super.onDestroy()
            Log.d(TAG, "fragment_onDestroy")
        }
    
        override fun onDestroyView() {
            super.onDestroyView()
            Log.d(TAG, "fragment_onDestroyView")
        }
    
        override fun onDetach() {
            super.onDetach()
            Log.d(TAG, "fragment_onDetach")
        }
    }
    
    

    总结

      如上封装的其实也是不够完善的,但是总体可以实现单Activity+多Fragment模式的项目,并且不需要引用第三方框架,当然上面也封装了关于使用Arouter路由的方式实现组件化的Fragment通信方式。通过上面的方式写了demo,具体实现可参考demo。
    Needle: 单Activity+多Fragment模式创建项目 (gitee.com)

    相关文章

      网友评论

        本文标题:Android 实现单Activity多Fragment页面项

        本文链接:https://www.haomeiwen.com/subject/addqurtx.html