I'm using the code from the Example section of this page http://developer.android.com/guide/components/fragments.html
In landscape mode, the activity displays two fragments side-by-side, and on rotation, it launches a new activity containing the "detail" fragment.
The problem is that if I add a menu item to the actionbar from the detail fragment then rotate the screen to portrait to launch the detail fragment in a new activity then exit the activity, the menu item is still displayed even though the fragment supplying the menu item has been removed.
I've tried removing the detail fragment with a FragmentTransaction in onResume and then calling invalidateOptionsMenu(), but it doesn't remove the menu item.
I'm using ActionBarSherlock, and I've also tried supportInvalidateOptionsMenu()
Has anyone else run into this problem?
I thought I was removing the fragment, but I wasn't. After removing the fragment in onResume, my problem was solved.
Related
How can i make the fragments on the backstack not to trigger the onOptionsItemSelected(). Because every time i create a new fragment it always trigger the onOptionsItemSelected() if i select an item in menu. Also this fragment will be reused throughout the activity so "return true" on onOptionsItemSelected() is not an option because the fragment that was created still the same with the backstack fragment.
It appears that onOptionsItemSelected will always be called in every new fragment that is added in activity Menu documentation , so to solve my problem is that I added a validation which appears this one
<CurrentFragment>this.equals(fragmentManager.findFragmentById(R.id.frame_container) to validate if the fragment displayed/ attached in activity is the fragment that trigger the onOptionsItemSelected.
Hope this will help to anyone who will have this kind of behaviour on fragments.
I have an activity with 3 tabs. I've used ActionBar.Tab class to add this tabs. Each of this tabs contain a fragment - and in this fragment I've added a menu overriding onCreateOptionsMenu.
When I open this activity the menu items are displayed only after I manually select a second tab, and after that the items are displayed as they should.
Can anyone please explain to my why this weird behavior and how I can overcome it?
Have you tried calling onCreateOptionsMenu in your activity?
It might be that it is not called until you start the second fragment so the menu is not inflated yet.
I have a fragment that hosts a ViewPager with 3 fragments, 2 of them contain ListView (not inherits FragmentList)
The problem is:
If I click on one of the items in one of the list the application navigates me correctly to a new fragment, but, if I press the back button and go back to the ViewPager, a can see a blank fragment, only the first fragment who didn't have ListView is created, none of the other fragments are displayed, nor even their onStart is called
What's wrong? I suppose the fragment is not attached again, how can I solve this issue?
I am developing an app that uses ActionBar navigation, I have 3 tabs (I dont use tabhost) as image below:
In the first tab, a fragment named FragmentListItem will be shown, if I click on Item1, FragmentListItem will be replaced by another fragment named FragmentItemDetails, that shows details of the selected item.
My problem is, when I select Tab2 or Tab 3, and then I reselect Tab1 again, what I get is not FragmentItemDetail anymore, it is FragmentListItem instead.
So, why is that? If I want FragmentItemDetail still be there instead of FragmentListItem, how to do that?
Moreover, when I press Back button (when Tab1 is selected), I want FragmentListItem will be shown again. I realise that Fragment class has no onBackPressed method to implement.
There are different solutions to this issue.
One possible solution is to use an own FragmentManager for this tab. This is possible with the getChildFragmentManager() method of Fragments.
Another solution could be a flag to identify which Fragment was shown as last on Tab1. If you know which Fragment was shown as last you could show this one by search this Fragment in your BackStack with the findFragmentByTag(tag) method of FragmentManager.
But I recommend you to try the first Option. If each tab has his own stack you have less work with your BackButton.
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 e.g. 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.