Git Product home page Git Product logo

Comments (9)

YoKeyword avatar YoKeyword commented on May 28, 2024

@LinkZhang getTopFragment方法里是
` SupportFragment getTopFragment(FragmentManager fragmentManager) {

    List<Fragment> fragmentList = fragmentManager.getFragments();

    if (fragmentList != null) {

        for (int i = fragmentList.size() - 1; i >= 0; i--) {

            Fragment fragment = fragmentList.get(i);

            if (fragment != null && fragment instanceof SupportFragment) {

                return (SupportFragment) fragment;

            }
        }
    }
    return null;
}

`
应该是你的场景下栈内找不到SupportFragment了;
另外谢谢你提醒,我会把每次开始Fragment事务时hack fix栈的顺序代码,放到popTo后,确保不会出现栈内顺序BUG
:)

from fragmentation.

YoKeyword avatar YoKeyword commented on May 28, 2024

@LinkZhang 多多反馈哈 有任何问题随时提出 :)

from fragmentation.

LinkZhang avatar LinkZhang commented on May 28, 2024

@YoKeyword 场景大概是这样连续pop多个fragment(A,B)再add(C),发现C在getfragments()里的位置是2,再add fragment分配的位置是0,1;导致获取的栈顶fragment是C而不是新add的。本以为是异步操作引起的,放在runnable里也没起作用,后来把getTopFragmeng()改成以下代码正常了

int count = fragmentManager.getBackStackEntryCount();
if (count >0) {
     String tag = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount() - 1).getName();
     Fragment fragment = fragmentManager.findFragmentByTag(tag);
     if (fragment != null && fragment instanceof SupportFragment) {
          return (SupportFragment)fragment;
      }
}
return null;

from fragmentation.

YoKeyword avatar YoKeyword commented on May 28, 2024

@LinkZhang 👍 非常非常感谢,这样处理确实最安全 ,明天我更新下哈,顺便把hack fix栈顺序的代码添加到pop后的地方,确保更安全 :)

from fragmentation.

YoKeyword avatar YoKeyword commented on May 28, 2024

@LinkZhang 从你的描述,C在2的位置,所以肯定是来没来得及pop A,B的时候,C就进栈了,这样就产生了异常~
如果pop后需要立刻start(C),你应该调用
popTo(Class fragmentClass, boolean includeSelf, Runnable afterTransaction)
这个方法,它可以确保你在出栈后立刻执行下一项Fragment事务时保证正常,比如你接着start(C)

from fragmentation.

LinkZhang avatar LinkZhang commented on May 28, 2024

@YoKeyword popTo()这个方法好像不能连to也pop,我需要清空所有fragment,所以我增加了一个popAll方法

 public void popAll(Runnable runnable){
        mActivity.preparePopMultiple();
        int count = mFragmentManager.getBackStackEntryCount();
        while (count>0){
            mFragmentManager.popBackStackImmediate();
            count--;
        }
        mActivity.popFinish();

        if (runnable!=null) {
            mHandler.post(runnable);
        }
    }

调用这个方法的确执行的是先pop后add,虽然BackStack已经为空了,但getFragments()并不为空,还是会出现新add的fragment顺序错误的问题,所以getTopFragment()通过顺序来取栈顶fragment会有问题

from fragmentation.

YoKeyword avatar YoKeyword commented on May 28, 2024

@LinkZhang 我不理解为什么要这么处理 连续pop单个Fragment,我以前好像是测试过,是不安全的,会引起一些异常~ 你现在的状况 BackSack为空 getFragments()不为空 说明出栈已经异常了
popTo(第一个Fragment,true,new Runnable(你的后续事务)) 这个方法我一直用的,目前没发现问题
你检查下 代码 用这个方法试试, 发生栈内异常就很危险了,应该从解决栈内异常解决这个问题 :)

from fragmentation.

LinkZhang avatar LinkZhang commented on May 28, 2024

@YoKeyword 是吗,一直没什么异常,我改成这样应该可以吧

 public void popAll(Runnable runnable){
        mActivity.preparePopMultiple();
        int count = mFragmentManager.getBackStackEntryCount();
        if (count>0) {
            mFragmentManager.popBackStackImmediate(mFragmentManager.getBackStackEntryAt(0).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
        mActivity.popFinish();

        if (runnable!=null) {
            mHandler.post(runnable);
        }
    }

不过,我刚刚测试了一下,getFragments()的顺序还是不对,getFragments里的顺序真的可以标记fragment的真实顺序?

from fragmentation.

YoKeyword avatar YoKeyword commented on May 28, 2024

@LinkZhang 异常不是报错,而是一些隐患,后续可能会导致一些不正常的情况
你getFragments的时机稍微延迟点打印下,Fragment相关的一些调试都需要稍微延迟下,或者通过API提供的查看栈视图 方法查看栈内的情况~ 我用popTo(事务)的那个方法执行后,查看栈视图(内部就是getFragments),一切正常

我早上更新了getTopFragment方法,换成了你的方式,以及HackFix的方法,放倒了popFix里

`private void popBackFix(Class<?> fragmentClass, int flag, final FragmentManager fragmentManager) {

    mActivity.preparePopMultiple();
    fragmentManager.popBackStackImmediate(fragmentClass.getName(), flag);
    mActivity.popFinish();

    mHandler.post(new Runnable() {
        @Override
        public void run() {
            FragmentTransactionBugFixHack.reorderIndices(fragmentManager);
        }
    });

}`

from fragmentation.

Related Issues (20)

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.