简体   繁体   English

Android每个片段更改setNavigationMode导致应用崩溃

[英]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 我创建了以下项目,因此您可以看到我的确切代码以及正在发生的情况: 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 下面是我用来在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. 我看到第二个和第三个片段都调用了onrefresh,但是第三个片段没有显示。

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} 以下是我通常会收到的错误03-27 15:26:31.029:E / AndroidRuntime(5505):java.lang.IllegalStateException:片段已添加:Fragment3 {41f2e390#2 id = 0x1020002fragment3}

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. 我有类似的情况-只是我使用NAVIGATION_MODE_LIST而不是标签。 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. 每当导航列表消失时,只要调用该片段,我就调用NAVIGATION_MODE_STANDARD ,而其他片段则调用NAVIGATION_MODE_LIST

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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