Android TabHost Fragment Switch - android

I'm using TabHost and I have a problem. What I want is first tab should be a FragmentActivity and it can switch to another Fragments. This application helps me but I have also questions. In this example, you can go to infinite another Fragments but it's happening by the help of launchNewFragment method and this method is called from xml. But I need to call this method programmatically because when button is clicked, I also want to move some values (such as selected values in dialog) to another Fragment. Or is there another way to solve this problem? I'm waiting your suggestions.

You might want to use Action Bar with Tabs instead of TabHost. You can get information about how to use it here: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs
No matter on the chosen solution, replacing the displayed fragment in an activity is done like this:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, new MyFragment());
transaction.commit();
You just have to add this to the button listener and replace MyFragment with your fragment.

Related

Which method is called if I reopen a Fragment?

I have the following problem:
I have three fragments that I can switch through swiping. The first contains images that are regularly made visible and invisible by an external method. However, I see the change in the visibility of the images only when I leave the fragment by swiping and then swipe back again.
Therefore, the question is which method is called when rewinding back, so that I can call this explicitly if the fragment is visible.
I think this will help when you back on the fragment.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();

Android Fragments switch without destroy

I have an activity with 3 fragments. Every fragment has a question.
When i create the activity I'm calling .add(...) method for the first fragment. For the others i call .replace(...) method with addToBackStack(...) method.
So i have this situation in the backstack:
.add(Frag1)
--> Frag 1
.replace(..., Frag2, ...)
-->Frag2
-->Frag1
.replace(..., Frag3, ...)
--> Frag3
--> Frag2
--> Frag1
Now, when I'm at the third fragment and then go back I'm popping it from the stack. I want to preserve the third fragment so if i go back to the second and then go next to the third i want to have the question answered and not to call again another onCreate().
I want to create a sort of switch of the fragments. Is it possible? Can someone help me?
You can create a fragment once and Show/Hide it depending on your needs.
See hide & show from FragmentTransaction docs
Also see this question
How about you use viewpager instead, you will need to:
Disable paging by swipe, so you can change the page programmatically
Load all pages at once (set setOffscreenPageLimit your pages count), so that each page keeps its state

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!

How to reuse of fragment with multi-tabs

I have developed project using Fragments and Tabs at Bottom,
I use just one activity and all fragemnts and tab click is managed by tabHost and stack of fragment
I use this code github link fragment_tab_study for setting tabs and maintaining stack of fragment
Now the issue is that we just push fragment(i.e replace fragment) so every time it call its onCreateView and all other method, I think this is a very big issue in this demo, as we click second time on tab1 it should only call its onResume method as happen in activity
is any solution for this ?
If you use replace of then in any case you have to start with onCreateView. If you dont want to recycle you can choose add method of transaction. But to replace is better in fragment terminology. You can even save transaction between fragment in back stack.
Please let me know if you have some other concern regarding fragment.
Thanks
Ankur

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.

Categories

Resources