Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does popbackstack call onCreateView of the fragment?

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();
like image 825
Minh Tran Avatar asked Feb 11 '14 17:02

Minh Tran


People also ask

What is onCreateView fragment?

onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.

What is called after onCreateView?

The onActivityCreated() method is called after onCreateView() and before onViewStateRestored() . onDestroyView() : Called when the View previously created by onCreateView() has been detached from the Fragment . This call can occur if the host Activity has stopped, or the Activity has removed the Fragment .

Does popBackStack destroy fragment?

FragmentManager popBackStack doesn't remove fragment.

How can we prevent onCreateView when back button is pressed in fragment in Android?

In order to prevent overlapping you might just set android:background="@color/white" in the root view of your second fragment to make it not transparent.


1 Answers

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

like image 123
Andrew Matiuk Avatar answered Sep 21 '22 11:09

Andrew Matiuk