Android changing setNavigationMode per fragment is crashing the app - android

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.

Related

Using ViewPager and PagerTabStrip - extending FragmentActivity

First - sorry for the newbish question.
I've started building an app that has a single Activity and a navigation drawer. Each menu item loads a new fragment in the middle frame layout.
I want to create another fragment that:
has tabs
allows for swipe scrolling
It seems like the only way to do this is to create add a ViewPager and PagerTabStrip. All the tutorials I've read indicate the ViewPager requires extending out to FragmentActivity. I have a few questions:
Am I doing anything wrong by replacing the fragment content when navigating menu options?
If what I am doing is ok, is there anyway to incorporate swipe navigation without calling FragmentActivity?
If I need to use FragmentActivity for this one page, I'm assuming I'll call change pages via Intent. Doing so would result in losing the click actions in the navigation drawer. Do I have to call (or duplicate) my code from one activity to another?
EDIT
Sorry about the confusion. Here's what I'd like my application do:
Open app. MainActivity starts. Navigation drawer loads. Main content is loaded via a fragment.
User opens navigation drawer and selects this new menu item I'm creating. It is a new fragment that loads in the frame (like the other menu items). However, it has tabs and supports swiping.
ViewPager is just usual descendant of View so it can't require using of FragmentActivity.
It's absolutely ok.
You don't need to use FragmentActivity. I suppose you just read tutorial about "Implementation of drawer" where author of the tutorial used FragmentActivity.
Can't understand what do you mean. Pages of ViewPager is just views not activities. You don't need to use Intent.
PS Actually I can't understand your problem at all. It's absolutely unclear why you don't want to use FragmentActivity.

onCreateOptionsMenu of Fragment called twice

Scenario:
I using a ViewPager inside a Fragment. This ViewPager inflates 3 fragments (AFragment, BFragment, CFragment) pages using FragmentPagerAdapter.
I have printed logs inside
onCreateOptionsMenu
onPrepareOptionsMenu
of all 3 fragments.
Problem:
For AFragment when created onCreateOptionsMenu was called only once,
but when I swiped to BFragment onCreateOptionsMenu and
onPrepareOptionsMenu for BFragment were called twice. And similar
thing happened with CFragment.
Can anyone explain why this is happening and How I can avoid it?
Thanks.
Because android by defaults loads a next and previous fragment in background, in order to load fragments efficiently and to remove lag.
However you can customise the same by using setOffscreenPageLimit (int limit), Which is used to set the number of extra fragments you would like to load in background. By default its set to 1.
Read Android fragments strange load for more information

android fragment fires itself when using tabs

I have this Android architecture:
Activity that has an action bar with two tabs.
each tab is a fragment
my problem is, when i start the activity, the two tabs start working.
I don't want the second tab to start working unless i click on it.
How please?
note that i am using onCreateView on my second fragment
note
i tried to make my functions on onResume() but still fires itself
This comes from caching of the ViewPager : it will initiate the visible fragment and one on each side (left and right). There is no thing you can do against that. The method setOffscreenPageLimit will refuse a value below 1.
So the solution is to fire your own even to actually "start" your fragment when it becomes visible. Don't use the fragment lifecycle per say, but add a custom method : startBeingVisible().
To fire this custom event, you can just use an OnPageChangeListener.

Android actionbar tabs: content of each tab is recreated everytime I click on that tab

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.

ActionBarSherlock TabNavigation

I have gotten my tabs up and running with Sherlock Fragments. So i have two tabs and two fragments. Now what i want is that when i click on a button in one of the fragments, I want another fragment to open up, but it should be inside the tab that contained the original fragment. Can anyone show me how this can be done ?
Actually Nested Fragments are not possible directly. I had a similar issue and but the Fragment to be shown was small and simple. So I just played with setVisibility() as LinearLayout.GONE and LinearLayout.VISIBLE
On the Fragment initialization, I hid the second layout and when I need to show the other fragment, I hid the first layout and showed the second layout. It worked great for me!

Categories

Resources