Giter Club home page Giter Club logo

fragmentnavigatorhideshow's Issues

求教两个疑惑

首先感谢作者提供的思路,疑惑有二:
1.NavHostFragmentcreateFragmentNavigator()方法高版本已废弃了,有其他思路吗
2.containerId在false条件下的nav_host_fragment_container报错,根据备注理解是需要开发者自行填写Fragment容器布局的Id么。
麻烦了。

发现每次都会创建新的fragment实例,达不到复用的效果

最后修改了FragmentNavigatorHideShow的隐藏显示片段,好像正常了
` //region 添加的代码

    var frag: Fragment? = mFragmentManager.primaryNavigationFragment //查找当前导航栈顶的fragment,当前的flag
    if (frag != null) {//如果当前存在,就hide。
        ft.setMaxLifecycle(frag, Lifecycle.State.STARTED)
        ft.hide(frag)
    }
    //查找目标导航fragment 如果查找到了就show这个fragment,如果没有查找到就创建一个新的fragment。
    val tag = destination.id.toString()
  var  navfrag = mFragmentManager.findFragmentByTag(tag)//要导航的flag
    //如果当前fragment == 目的地fragment,同样创建  这里处理我打开我自己的逻辑,判断是否需要重新创建一个新的 frag。
    if (frag?.javaClass?.name == className) {
        ft.setMaxLifecycle(frag, Lifecycle.State.RESUMED)
        ft.show(frag)
    } else if (navfrag != null) {
        //fragment 已经存在显示
        ft.setMaxLifecycle(navfrag, Lifecycle.State.RESUMED)
        ft.show(navfrag)
    } else {
        //fragment 不存在创建,添加
        navfrag = mFragmentManager.fragmentFactory.instantiate(mContext.classLoader, className)
        navfrag.arguments = args//设置参数.
        ft.add(mContainerId, navfrag, tag)
    }
    //endregion

    //ft.replace(mContainerId, frag) //注释掉原有逻辑
    ft.setPrimaryNavigationFragment(navfrag)//将新的目标fragment标记为栈顶。可以这么理解。
    ft.setReorderingAllowed(true)
    return ft`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.