【问题标题】:How to prevent fragment's recreating problem when oriantation change?方向改变时如何防止碎片产生问题?
【发布时间】:2021-02-14 19:33:12
【问题描述】:

在我的应用中,我使用带有导航组件和 mvvm 架构的导航抽屉。

我的问题是屏幕旋转片段重新创建时。如何防止这种情况?请提供任何建议或示例代码?

我的代码是这样的:

主要活动:

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)


        drawerLayout = findViewById(R.id.drawer_layout)
        navView = findViewById(R.id.nav_view)


        navController = findNavController(R.id.nav_host_fragment)
        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.nav_home,
                R.id.nav_productInsight,
                R.id.nav_turkey,
                R.id.nav_usa,
                R.id.nav_china
            ), drawerLayout
        )
            setupActionBarWithNavController(navController, appBarConfiguration)
            navView.setupWithNavController(navController)
            navView.setNavigationItemSelectedListener { menuItem ->
            when (menuItem.itemId) {
                R.id.nav_home -> {

                    navController.navigate(R.id.nav_home)

                    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                        drawerLayout.closeDrawer(GravityCompat.START)
                    }
                    true
                }
                R.id.nav_productInsight -> {

                    navController.navigate(R.id.nav_productInsight)

                    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                        drawerLayout.closeDrawer(GravityCompat.START)
                    }
                    true
                }
                R.id.nav_turkey -> {
                    val menuItemView = findViewById<View>(R.id.nav_turkey)
                    showPopupTurkey(menuItemView)
                    false
                }
                R.id.nav_usa -> {
                    val menuItemView = findViewById<View>(R.id.nav_usa)
                    showPopupUsa(menuItemView)
                    false
                }
                R.id.nav_china -> {
                    val menuItemView = findViewById<View>(R.id.nav_china)
                    showPopupChina(menuItemView)
                    false
                }
                R.id.nav_logout -> {
                    showLogOutDialog()
                    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                        drawerLayout.closeDrawer(GravityCompat.START)
                    }
                    true
                }
                else -> {
                    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                        drawerLayout.closeDrawer(GravityCompat.START)
                    }
                    false
                }
            }
        }

    }

ViewModelClass:

class HomeViewModel @ViewModelInject constructor(
    private val dataUseCase: DataUseCase,
    private val networkHelper: NetworkHelper
):ViewModel(){

    fun getCountryProductionInformationChartList (filterId : String?,productId:String?) : LiveData<ResultData<BaseChartModel<ProductionQuantityModel>?>> {
        return flow {
            emit(ResultData.Loading())
            try {
                emit(dataUseCase.getCountryProductionInformationChartList (filterId,productId))
            } catch (e: Exception) {
                e.printStackTrace()
                emit(ResultData.Exception())
            }
        }.asLiveData(Dispatchers.IO)
    }

}

我的家庭片段:

@AndroidEntryPoint
class HomeFragment : BaseFragment() {


    private lateinit var mContext: Context
    private lateinit var mView : View


    private val mainViewModel: HomeViewModel by viewModels()

    private val mainObserver = Observer<ResultData<BaseChartModel<ProductionQuantityModel>?>> { resultData ->
        when(resultData) {
            is ResultData.Loading -> {
                mView.scrollView.visibility = View.GONE
                emptyDataText.visibility = View.GONE
                showLoading()
            }
            is ResultData.Success -> {
                hideLoading()
                mView.scrollView.visibility = View.VISIBLE
                emptyDataText.visibility = View.GONE
                resultData.data?.let {
                    setQtyChart(mView, it)
                    setPPMChart(mView, it)
                    setKPIChart(mView, it)
                    setBreakDownChart(mView, it)
                }
            }
            is ResultData.Failed -> {
                hideLoading()
                mView.scrollView.visibility = View.GONE
                emptyDataText.visibility = View.VISIBLE
                emptyDataText.text = resultData.message
            }
            is ResultData.Exception -> {
                hideLoading()
                mView.scrollView.visibility = View.GONE
                emptyDataText.visibility = View.GONE
                Toast.makeText(mContext,"Error!",Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_home, container, false)
        mView = root
        getProductionQuantities()
        return root
    }

如何处理这个重新创建的问题? 顺便说一句,安全 args 数据正在消失。

【问题讨论】:

    标签: android mvvm rotation fragment kotlin-coroutines


    【解决方案1】:

    您可以在您的活动中使用setRetainInstanceState(true)See more

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-20
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 2011-01-17
      • 1970-01-01
      相关资源
      最近更新 更多