ViewPager not for all activity fragments - android

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!

Related

How to save fragments states to avoid re-creating them

I've implemented something like the below picture:
I have some Tabs and every tab may have some buttons which load some different fragments. In this situation, in Tab 1, Button 1 loads Fragment 1, and Button 2 loads Fragment 2 and each time the Fragments will be re-created. but I want Buttons to load Fragments just once and avoid re-creating next times.
Button 1 current onClick:
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container_root, FragmentOne.newInstance(),"Frag1");
transaction.commit();
How can I save fragments states to avoid reloading every time?
EDIT
The Tab 1 and Tab 2 Fragments are inside a ViewPager so I could not use ViewPager for Fragment 1 and Fragment 2.
Use below code
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.add(R.id.container_root, FragmentOne.newInstance(),"Frag1");
transaction.addToBackStack("Frag1");
transaction.commit();
instead of
FragmentTransaction transaction =
getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container_root, FragmentOne.newInstance(),"Frag1");
transaction.commit();
Problem:
You are trying to replace fragment while adding them via FragmentTransaction. If you need to keep instance of them you need to use transaction.add method.
Caution:
Using this method can overlap fragments. So you need to add backgroundcolor to both fragments and make the parent layout as clickable so that you can interact
you can create your tabs with viewpager
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setOffscreenPageLimit(tabsNumber - 1); // e.g. send 2 as parameter for 3 tabs
and save your tab state

Android Fragment overlapping each other and showing transparent view

I have an activity with a fragment container layout and in that layout i load a fragment A which has a listview (listview loads data from web). Now on clicking listview item i want to show detail view. For that i am loading another fragment B which is detail fragment. Now to do that first i used
FragmentTransaction ft = ((FragmentActivity) context).getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragmentsContainerLayout, new ScheduleDetailsFragment());
ft.addToBackStack(null);
ft.commit();
But using replace remove the previous fragment A which contain listview and on pressing back it again reloads the onCreateView method of fragment A and its listview data. I want to prevent it from loading fragment A again (or sending call to webserver) on backpress. I want to maintain fragment A state and its listviews scroll position. To do that i used second option which is
FragmentTransaction ft = ((FragmentActivity) context).getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragmentsContainerLayout, new ScheduleDetailsFragment());
ft.addToBackStack(null);
ft.commit();
If i use add() then Fragment B loads on top of Fragment A and Fragment A still visible. It shows both fragments overlapping each other and transparent. I also came across anothe solution which is to use show() and hide() methods. But i dont think that is an efficient way to achieve that. Can anyone suggest an effective solution?

How to switch Tabs without Animating Fragments?

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.

Proper top level navigations in android fragments like in Google Play

I have an Activity with navigation drawer and a default fragment set in to Activity when application starts.
I have 4 top level navigation in my navigation drawer
Fragment 1
Fragment 2
Fragment 3
Fragment 4
and switching the fragments inside the activity on click on each each navigation. I want implement the fragment navigation in such manner that from each top level navigation fragment, if user clicks back button it should first come to Main or default fragment and from there app should exit same like in Google Play. I call it master fragment.
eg:
Default(master) Fragment > Fragment 1
Fragment > Fragment 2
Fragment 2 -- Back pressed > Deafult fragment and like so.
What I have tried so far :
I have tried adding fragment in backstack but it doesn't help it takes me all fragment in stack.
getSupportFragmentManager().beginTransaction()
.add(R.id.container, selectedFragment)
.addToBackStack("naviagtion_stack")
.commit();
My each top fragments also have child fragments in stack so stack count also did not help me.
I don't want to remove and add my default fragment because as it fetches some data from network so recreation will make the network call again which i don't want .
I want exactly what Google Play does. I just want to know the logic.
Add your master fragment to backstack and remember the tag: fragmentManager.beginTransaction()
.add(R.id.main_layout, masterFragment)
.addToBackStack(INITIAL_STATE)
.commit();
Click on the navigation elements should do following before adding a corresponding fragment: fragmentManager.popBackStack(INITIAL_STATE, 0);
This call removes from backstack everything but your master fragment.
All fragment transactions (including navigation fragments) should generally do the same thing, for example:
fragmentManager.beginTransaction()
.add(R.id.main_layout, fragment)
.addToBackStack(null)
.commit();

Dynamic display using Fragments and FragmentTabHost?

I am working on an application that contains three tabs and each tab has a fragment associated with it. On certain conditions i have to display child tab within second parent tab. But if that condition is not true then child tabs should not load and the parent tab should load one of the fragment i used in child tabs. For example, i have three fragments FragmentA, FragmentB, and FragmentC. Now if the condition is true then i will display tabs and load FragmentB and FragmentC inside FragmentA using Fragment tab host. But is condition is false i want to display FragmentB in FragmentA.
I tried using Fragment.replace() but it fails.
I think this is not a completely new issue.
You can find an answer here:
Separate Back Stack for each tab in Android using Fragments
Regards
Francesco.
In my intention you should override the onHiddenchange() for fragment A and check the condition over here.
I was able to solve the issue using a common fragment. In FragmentA's onCreateView if condition is true create Tabs using FragmentTabHost and add FragmentB and FragmentC to the tab. If condition is false then inflate a view using
View fragmentView = Inflator.Inflate(R.id.fagment_layout, FragmentB);
FragmentB fragmentB = new FragmentB();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(fragmentView, fragmentB);
transaction.commit();
It worked for me.

Categories

Resources