首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止原点发生变化时碎片的再创造问题?

如何防止原点发生变化时碎片的再创造问题?
EN

Stack Overflow用户
提问于 2020-11-02 06:04:30
回答 1查看 42关注 0票数 0

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

我的问题是什么时候屏幕旋转碎片重新创建。如何防止这种情况发生?请问有什么建议或示例代码吗?

我的代码就像:

主要活动:

代码语言:javascript
复制
 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 :

代码语言:javascript
复制
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)
    }

}

My Home片段:

代码语言:javascript
复制
@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数据正在消失。

EN

回答 1

Stack Overflow用户

发布于 2020-11-02 13:44:56

您可以在活动中使用setRetainInstanceState(true)查看更多信息

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64640546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档