Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragment backStack issue

I have the following code in my Activity:

     public void categoryClicked(int categoryId, String categoryName) {                 


ItemList newFragment = ItemList.newInstance(categoryId);
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.replace(R.id.itemContainer, newFragment);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    ft.addToBackStack(null);           
                    ft.commit();
            }

It works as expected, I am able to go back after clicking a few times to the previous states. However, if I only go one deep, I get the following exception:

03-10 22:17:19.895: ERROR/AndroidRuntime(23075): java.lang.IllegalStateException: Content view not yet created
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.app.ListFragment.ensureList(ListFragment.java:377)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.app.ListFragment.getListView(ListFragment.java:277)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at com.xxxxx.fragment.ItemList.onActivityCreated(ItemList.java:67)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:749)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:921)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.app.BackStackRecord.popFromBackStack(BackStackRecord.java:639)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1254)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:402)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.app.Activity.onBackPressed(Activity.java:2057)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.app.Activity.onKeyDown(Activity.java:1953)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.view.KeyEvent.dispatch(KeyEvent.java:2335)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.app.Activity.dispatchKeyEvent(Activity.java:2236)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1648)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.view.ViewRoot.deliverKeyEventPostIme(ViewRoot.java:2682)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2655)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1952)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.os.Looper.loop(Looper.java:126)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at android.app.ActivityThread.main(ActivityThread.java:3997)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at java.lang.reflect.Method.invokeNative(Native Method)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at java.lang.reflect.Method.invoke(Method.java:491)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075):     at dalvik.system.NativeStart.main(Native Method)

So basically, if I only call the replace once, it errors out on my when I hit the back button.

Also, with ListFragment, do I have to set the background to white? I didn't touch it and I can see the old listview showing through....

Thanks!

like image 246
runor49 Avatar asked Mar 11 '11 03:03

runor49


1 Answers

Well, the problem you are running into is there are no fragments to go back to. This is what you can do. 1: Check to see if the fragment exists in the backstack (by giving it a name) and then disable going back 2: When you first start you program, add a fragment to the backstack, like an initial state before its clicked. Even in this situation though, your still going to need to do a chck to see if there is anything left in the backstack to go back to.

like image 54
Shaun Avatar answered Sep 27 '22 17:09

Shaun