How to switch Tabs without Animating Fragments? - android

I have an application with a stack of Fragments added on top of each other.
The Fragments are animated to slide in when opened, and slide out when popped from the backstack. Here's a sample of where I open each Fragment:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_right,0,0,android.R.anim.slide_out_right);
ft.add(R.id.examplecontainer, examplefragment);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
The animations work as expected, and everything is good.. except that my App also has a couple more Tabs managed by a FragmentTabHost.
When I switch to another Tab and come back, I want the top Fragment to appear without any animations. Instead, it slides in again.
Is there any way to disable animations when switching between tabs on FragmentTabHost? Or any way to remove/change the custom animations set to a fragment?
Thank you in advance for any hints/suggestions.

Related

ViewPager not for all activity fragments

So I saw on an android developer site the example implementation of a ViewPager. I saw that ViewPager must be set in an activity class. My application has only one Actvity with about 15 Fragments. The thing I want to do is to use ViewPager only for 3 Fragments, for example:
I go to Fragment 5
When I click back I go to Fragment 1 (this is yet implemented)
When I swipe in Fragment 5 from left to right I go to Fragment 4
When I swipe in Fragment 5 from right to left I go to Fragment 6
When I swipe in the Fragment4/Fragment6 I go back to Fragment 5
When I swipe in the rest of Fragments, nothing happens
Is this even possible with only one Activity containing these all fragments? I will be grateful for any kind of hints.
PS: If that is gonna be helpful answering my question, this is how I switch between fragments now:
MealInfoFragment fragment = new MealInfoFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(MainActivity.currentFragment);
fragmentTransaction.commit();
Just for the record, I did it by creating a viewpager in a fragment that contains other 3 fragments!

Android fragment transition on back navigation to previous fragment

I have 2 fragments the first contains a button which on clicked opens a fragment with a ListView in it. I have a shared element transition for the button to transition into the new fragment (root layout) but I would also like to have this transition in reverse (the list fragment contracts into the button again).
However currently I detect the list item click and send an event to the Activity which pops the listview fragment off the backstack (popBackStackImmediate()) hence does not show the transition.
Is there a good way to allow back navigation while preserving reverse transition to work as well?
did you try this:
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack();
fm.executePendingTransactions();
PS: use getSupportFragmentManager() if you're using the support library else use getFragmentManager().

Android: Hide viewPager tabs when launching a fragment from one of the viewPager tab fragment

I have a viewPager in my app which has several tabs. In some of the tabs, on clicking an item a new fragment is shown. I want this fragment to cover the tabs. Doing this is possible but the approaches don't look good to me.
1.) one way is that I add the newly created fragment to the activity using getSupportFragmentManager(). This solves the problem but doesn't look like a good idea as it will create problems when using back button etc.
2.) Other way is to hide the tabs manually using Visibilty.GONE but problem with this approach is that this hiding of the tabs is visible, I mean the animation could be seen and looks bad.
Is there a better approach to do this problem?
This is my code. "sub_fragment_container" is present in the activity xml, so I get an error java.lang.IllegalArgumentException: No view found for id 0x7f0e00ff (com.my.app:id/sub_fragment_container) for fragment DetailFragment{
FragmentTransaction fragmentTransaction = getChildFragmentManager()
.beginTransaction();
Fragment profileFragment = new DetailFragment();
profileFragment.setArguments(bundle);
fragmentTransaction
.add(R.id.sub_fragment_container, profileFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Use android:animateLayoutChanges="true"
Or, to fine tune it further, here's a tested method that I've personally used in my app Newslet to solve the exact same problem.
Wrap your Toolbar and TabLayout within an AppBarLayout. This should be easy if you're aware of the Design Support Library. If not, this should get you started.
Add this piece of code in your onCreate():
LayoutTransition layoutTransition = new LayoutTransition();
layoutTransition.setDuration(200);
layoutTransition.setStartDelay(LayoutTransition.CHANGE_APPEARING, 0);
layoutTransition.setStartDelay(LayoutTransition.APPEARING, 0);
layoutTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
layoutTransition.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, 0);
appBarLayout.setLayoutTransition(layoutTransition);
After this, when you remove the Tabs or add it back, the layout change will animate smoothly.
Hope this helped you!

Hiding Actionbar before fragment transaction (Android)

My application contains an activity with several fragments. The activity displays the ActionBar by default. However, there are several fragments, displayed in order, where the ActionBar should not show. For these fragments, I hide the ActionBar in the onCreate code with getActivity().getActionBar().hide(); . However, each time one of these fragments are loaded, the ActionBar flashes on the screen momentarily before disappearing.
How can I make the actionbar disappear before the fragments are displayed on the screen? For reference, below is the code I use for transactions between the fragments:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.commitAllowingStateLoss();
I would suggest two ways to solve this.
Reconsider your activity-fragment relation, should it be split into different activities, i.e. some activities with action bar, some without action bar.
Use toolbar, and remove all default actionbar, assign the toolbar to your fragments which need it instead of the activity. Be reminded that you are not using setSupportActionBar() as this is not part of the activity layout.

How do I retain the state of my ViewPager in my TabLayout when I'm replacing a Fragment on top of it?

I have a single activity that uses a bunch of different Fragments. I have a TabContainer Fragment that holds a TabLayout which uses a ViewPager to handle tab navigation. Each Tab is its own Fragment.
In one of my tabs, I want to tap and place a fragment on top of my Tabbed fragment. This is meant to be a "details" sort of screen, so I don't want the tabs to be visible. I'm using this and it works as intended:
fragmentTransaction.replace(android.R.id.content, fragmentToDisplay).addToBackStack(null).commit();
Now, when I navigate back, the content in my tab is empty. The content in the tab directly next to that tab is also empty. When I navigate two tabs away, the content is recreated and the normal functionality returns. Why is content not being recreated on the tabs initially when I remove my "detail" fragment?
It turns out that I was simply not passing the correct FragmentManager to the FragmentStatePagerAdapter.
I needed to call getChildFragmentManager() on the Fragment, not getSupportFragmentManager() on the activity.
Thanks to these two posts for the answer:
Fragment in ViewPager not restored after popBackStack
and: Replacing ViewPager with Fragment - Then Navigating Back

Categories

Resources