为什么popbackstack会调用片段的onCreateView?[英] Why does popbackstack call onCreateView of the fragment?

本文是小编为大家收集整理的关于为什么popbackstack会调用片段的onCreateView?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

试图弄清楚当我弹出片段时,为什么要调用oncreateview.

使用以下代码:

fragmentManager.popBackStack("tag1", 1); 
ft = fragmentManager.beginTransaction(); 
ft.remove(fragmentManager.findFragmentByTag("tag1")); 
ft.commit();

logcat:

FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 79, size is 79
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at edu.pacific.pocketrx.FlashCardFrag.onCreateView(FlashCardFrag.java:72)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
        at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:764)
        at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1562)
        at android.support.v4.app.FragmentManagerImpl$3.run(FragmentManager.java:495)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

删除片段的代码:

fragmentManager.popBackStack("tag1", 1);
ft = fragmentManager.beginTransaction();
ft.remove(fragmentManager.findFragmentByTag("tag1"));
ft.commit();

推荐答案

实际上fragment管理器不存储您的隐藏片段,它仅存储状态

类BackstackState实现包裹式

参见http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/support/v4/app/fragmentmanager.java#fragmentmanagerimpl.popbackstackstackstate%28android.os.handler%2cjava.lang.lang.lang.lang.string; p>

当您弹出片段时,Fragment Manager试图重新创建以前的视图以显示它.

参见Movetostate方法

其他推荐答案

更新:请忽略这个答案,这显然是错误的,我不知道我在说什么(这是3年前!)

原始(不正确)答案:

在我的情况下,我在后堆上有2个片段,称之为:

FragmentManager fm = activity.getSupportFragmentManager();
            fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

似乎也触发了onCreateView()即使是顶部的当前片段,这很令人困惑.我已经用PopBackStackimediate替换了它:

FragmentManager fm = activity.getSupportFragmentManager();
        fm.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

和onCreateView()现在不称为顶部片段.

本文地址:https://www.itbaoku.cn/post/2091058.html

问题描述

Trying to figure why onCreateView is called when I popbackstack the fragment.

Using the following code:

fragmentManager.popBackStack("tag1", 1); 
ft = fragmentManager.beginTransaction(); 
ft.remove(fragmentManager.findFragmentByTag("tag1")); 
ft.commit();

Logcat:

FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 79, size is 79
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at edu.pacific.pocketrx.FlashCardFrag.onCreateView(FlashCardFrag.java:72)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
        at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:764)
        at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1562)
        at android.support.v4.app.FragmentManagerImpl$3.run(FragmentManager.java:495)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

Code to remove fragment:

fragmentManager.popBackStack("tag1", 1);
ft = fragmentManager.beginTransaction();
ft.remove(fragmentManager.findFragmentByTag("tag1"));
ft.commit();

推荐答案

in fact fragment manager doesn't store your hidden fragment it stores only a state

class BackStackState implements Parcelable

see http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/support/v4/app/FragmentManager.java#FragmentManagerImpl.popBackStackState%28android.os.Handler%2Cjava.lang.String%2Cint%2Cint%29

and when you pop a fragment, fragment manager tries to recreate a previous view to display it.

see moveToState method

其他推荐答案

Update: Please ignore this answer it's clearly wrong and I had no idea what I was talking about (it was 3 years ago!)

Original (incorrect) answer:

In my case I had 2 fragments on the back stack, calling this:

FragmentManager fm = activity.getSupportFragmentManager();
            fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

Seems to trigger onCreateView() even for the current fragment on top which is quite confusing. I've replaced it with popBackStackImmediate:

FragmentManager fm = activity.getSupportFragmentManager();
        fm.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

And onCreateView() is now not called for the top fragment.