简体   繁体   中英

Android changing setNavigationMode per fragment is crashing the app

I created the below project so you can see my exact code and what is going on: https://github.com/CorradoDev/TabsTest/commit/8f054dab2371b791c4061ceb511413f720f65d67

Basically what I am trying to do is hide the tabs for some pages and show them in other pages.

Below is the code I am using to show the tabs in the onresume

       if(getActivity().getActionBar().getNavigationMode()==ActionBar.NAVIGATION_MODE_STANDARD){
        getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    }

THen to hide the tabs I am doing the below on resume:

 getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

When I am on the first fragment(nothing in backstack). I can show and hide the tabs on hte second. It gives errors sometimes with changing tabs.

When I am on the second fragment in the backstack and I hide the third fragment. I see the second and third fragment both call the onrefresh but the third fragment does not show.

I am confused on what is going on and why this is not easier.

Below is the error I generally get 03-27 15:26:31.029: E/AndroidRuntime(5505): java.lang.IllegalStateException: Fragment already added: Fragment3{41f2e390 #2 id=0x1020002 fragment3}

I still would like to know why the above does not work. But my fix was to create another activity with the fragment and no tabs. That seems to work well. But I am interested if they did not intend you to change tabs and no tabs per fragment.

I had a similar situation - only that I used NAVIGATION_MODE_LIST instead of tabs. I run into similar issues when I called a fragment from another fragment eg click on a list item opening up the item details.

Now I call all fragments from the main activity which allows me to control the set up of the actionbar. Whenever the navigation list should disappear I just call NAVIGATION_MODE_STANDARD when the fragment is called and NAVIGATION_MODE_LIST for the other fragments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM