I have main activity with 4 tabs in the settings tab I need to implement this flow:
The user click on a recyclerView item (this has been handled).
A new fragment is shown instead of the settings tab.
I tried to make a replace transaction on view pager view, but when the user click on another tab the newly created fragment is still shown and it's not replaced by view pager.
Edit: My code for showing the nested fragment:
clickListenr = settingsRecyclerAdapter.observeClickListener()
.subscribe(id -> {
if(id == 3){
getFragmentManager().beginTransaction()
.replace(R.id.view_pager,new WebViewFragment())
.addToBackStack(null)
.commit();
}
});
I suggest you to use only Framelayout in your main fragment. Then Creat two fragment named like MainFragmentTop and , SettingFragment(nested fragment). Then in your main fragment onviewcreated method relapce your MainFragmentTop fragment into framelayout. Then when you want to open setting fragment replace or add same into framelayout. So, it's become easier to use.
This is working fine.
Related
Im working with fragments on my main activity I have a Edit Text and buttons in my main activity But when I opens fragment page and click exact at the spot of Edit Text the keyboard activates.
So it means the below activity is also in working at same time
Fragments are designed to be inside an activity, if you don't want to make your activity visible you can use a full screen layout for fragment, to remove the clicks you can replace .add command with .replace when transacting to fragments give a background color to the root view of fragment layout also. I think this would help
YourFragment fragment = new YourFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.ll_job_view_fragment_container, fragment)
.addToBackStack(TAG_FRAGMENT_YOUR_VIEW)
.commit();
Edit
The replace may not help, If you inflate different fragments in the same container you just give the attribute android:clickable="true" to the top most parent view of each fragment that will help.
I'm going to develop an android app to use Fragments with a ViewPager using the FragmentPagerAdapter. I'm trying to replace fragments onClick of a button inside a fragment but when I click on that button, the current layout goes up and next layout still comes on that position i.e.2 layouts come on a single fragment and I don't want it.
I'm using these steps to replace but not getting changed properly,
FragmentTransaction transaction = getActivity()
.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.select_program_root, newFrag);
transaction.addToBackStack(null);
transaction.commit();
Even I'm using TabHost with ViewPager inside a horizontal ScrollView. Everything is fine in my app. I'm only stuck on replacing of fragments onClick of buttons which are located inside fragments.
Thanks in advance.
Finally, I solved it by implementing few lines of code.
Create this method into your FragmentActivity:
public void setCurrentItem(int item, boolean smoothScroll) {
mViewPager.setCurrentItem(item, smoothScroll);
}
And call it onClick of a Fragment:
((MainActivity) getActivity()).setCurrentItem(3, true);
I want to be able to replace the current fragment which is loaded via the actionbar tabs using a ViewPager with another fragment that is not connected to any tab like this
getSupportFragmentManager().beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.replace(id,frag , "VideoEdit")
.commit();
however when I do this it loads the fragment all right but the tabs don't work anymore and everytime I click on a tab it just stays on this fragment. I managed to find this gist
https://gist.github.com/andreynovikov/4619215 but I'm not sure it's the same thing I'm looking for and I don't want to try it out for fear of messing up my existing code with it.
You should avoid transitions like that.
I'll recomend to use FragmentWrapper.
One more fragment that you placed inside Tab it contains only FrameLayout as a container for other fragments. In this fragment, you could use childFragmentManager to add current Fragment and replace it with VideoEdit.
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.
My problem
I am using a ViewPager to display fragments inside a FragmentActivity. ViewPager gets fragments from the attached FragmentPagerAdapter.
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mAdapter = new HomePagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
Suppose ViewPager has 3 fragments to display: Fragment1, Fragment2, Fragment3. Fragment1 is a grid fragment and displays a grid. Fragment2 and Fragment3 have their own content to display.
When the use swipes on the screen, ViewPager will display the next fragment - Fragment2. And so on.
What I want?
What I want is that, when an item from the grid (displayed by Fragment1) is clicked, Fragment1 should be completely replaced with some other fragment, say Fragment4 (this is different fragment and is not returned by the attached adapter). User will work on Fragment4 and after ButtonBack click (just button iside Fragment4), Fragment1 with the grid should be displayed again. Meanwhile ViewPager should behave the same i.e on swipe, the next fragment (in our case Fragment2) will be displayed.
So I just want to get the same behavior as in example:
http://developer.android.com/training/basics/fragments/fragment-ui.html#Replace
My question
So, is this possible to achieve? If yes, then how to?
I would greatly appreciate for your help. Alex.
P.S. Sorry for my English:)
I've made a little example that shows how to achieve it:
https://github.com/danilao/fragments-viewpager-example
I think the point is to use another fragment as a container.
One way to achieve this is by adding another FragmentActivity that displays your Fragment4. Use Intent to start this activity and sent grid item position or other data in Extra.
On press of backbutton this activity will be finished and last activity with view pager will be displayed.
[EDIT] Old response. Better solutions are provided in other answers,
You will have to do the fragment transaction and add the existing fragment into backstack.
I created a sample project to show you how to do it.
The code is not fine tuned, I just created it to show you how to do it.
https://drive.google.com/folderview?id=0BxHClVwHSqq5dVRvT1Qyd0hYN0k&usp=sharing
But please be aware that even if you press back at any of the view pager screen, the previous fragment ends up showing as it is the same activity.
IF this is not expected behavior, then you should consider having multiple activities instead ( like tab layout )