I am trying to get an application working that uses the ActionBar slider with a TabHost. I am replacing the current Fragment with another when a menu item is selected.
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
The problem that I am getting is that I am unable to replace a fragment with a FramgentActivity which contains a TabHost. So my question is, is it possible to put a FragmentedTabHost inside a Fragment (instead of a FragmentedActivity)? If so, how would you implement it?
Related
I did so much researches in the internet and the conclusion was that we cannot use BottomNavigationView and ViewPager and TabLayout simultaneously. but there are a lot of app such as Instagram that merged them.
my question is that how can they do that? do they use a special trick or not?
you can create bottomNavigationView in activity and attach fragment in it with this code when click a on item of it:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction()
.replace(frameId, fragment, "fragTransaction");
transaction.commit();
and then create a fragment wiht a tabLayout and viewPager to control child fragment in it and you most using to control fragment in viewPagerAdapter
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction()
.replace(frameId, fragment, "fragTransaction");
transaction.commit();
It is not difficult to use bottomNavigationView with ViewPager.
In your layout declare both of these controls.
Setup viewPageAdapter and add fragments to it, set adapter to viewPager.
Setup bottomNavigationView overriding onNavigationItemSelectedListener, which now will be setting current item of viewPagerAdapter.
Do you need more detailed code examples, or you are ready to rock?
I've done some research but I really couldn't find the answer.
I have single activity with side menu, and holder. I have many (non support) fragments, and all of them are using same holder (one at a time).
When user uses menu (it's in main activity), and goes another page, I want to add name of the current fragment to backstack (using .addToBackStack(Fragment1.class.getName())), but I couldn't find how to get current fragment.
I don't want to implement interface etc to keep track of current fragment. There is a super simple way using fragmentManger isn't there?
You can get your current fragment like this:
if (getFragmentManager().getBackStackEntryCount() > 1) {
Fragment f = getFragmentManager().findFragmentById(R.id.content_frame);
if (f instanceof BlankFragment) {
// Do something
}
}
OK,
If you want to get latest entry from backstack(thanks to #AndroidGeek);
fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount()-1);
and, if you want to get currently active fragment (thanks to #Salman500 #AndroidGeek);
Fragment f = getFragmentManager().findFragmentById(R.id.fragment_holder);
you can use this to get fragment id for non support fragment
Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment_id);
if(fragment!=null)
{
getFragmentManager()
.beginTransaction()
.addToBackStack(null)
.commit();
}
You can keep track of fragments in the main activity (with variables) and access them there. Example:
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction= manager.beginTransaction();
MyFragment myFragment = new MyFragment();
myFragment.doSomething();
Adding to the back-stack:
FragmentTransaction fragment = getSupportFragmentManager().beginTransaction();
fragment.addToBackStack(fragment);
fragment.commit();
This is answered here: get currently displayed fragment
Just use addToBackStack() before you commit() the fragment trancsaction. See it here
So your code will look like :
...
fragmentTransaction.replace(R.id.holder, newFragmentToShow, newFragmentTag);
fragmentTransaction.addToBackStack();
fragmentTransaction.commit();
...
EDIT : after OP was edited
You do not need the fragment class to call addToBackStack() as you have mentioned in the OP. The String argument is an optional string just to keep the state for the backstack state. You should see the documentation
It is all internally managed and the current active fragment is automatically added to the backStack, you may call it from where ever you want, it will always use current active fragment.
I am using drawer navigation where all pages are fragments.How to open fragment page using button on another fragment ?
Use FragmentManager like
call this method button onclick method
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
you have to replace you fragment manager id instead of R.id.frame_container
I hope this one will help to you :)
I am develop the app using the fragments and i am facing one fragment1 adding to the another fragment2
fragment1 onclicks performing fragment2 .I didn't findout the solution for that. please guide anyone know
I am using the following code to add the fragment
Fragment2 fragment2=new Fragment2();
FragmentManager fragmentManager = activity.getFragmentManager();
android.app.FragmentTransaction ft=fragmentManager.beginTransaction();
ft.add(R.id.container,fragment2);
ft.hide(new Fragment1());
ft.addToBackStack(Fragment1.class.getName());
ft.commit();
The above to use adding onefragment1 adding to fragment2.Fragment1 onclicks perform to fragment2 . i didn't findout mistake.
please guide me anyone know .Advance thanks to all
nested Fragments are not recommended and for adding a fragment to a container from another Fragment use the parent Activity to do so , you can define a function inside Parent Activity which replace current Fragment with your second Fragment and call it from your first.
Adding a fragment to another fragment is the concept of nested fragments and not recommended. You should replace the fragment instead of adding. Use the following:
Fragment fragment = new Fragment2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().addToBackStack("fragment").replace(R.id.frame_container, fragment).commit();
I've been working with fragments for a while now, but I regularly encounter a problem that just annoys me. Fragments remain drawn over each other some times. Now, I managed to isolate one use case for this, and it goes like this:
Add Fragment A (also use addToBackStack with a name "backstack_state")
Replace Fragment A with Fragment B (use addToBackStack)
Replace Fragment B with Fragment C WITHOUT using addToBackStack
at a given point use popBackStack("backstack_state", 0) and here comes the issue:
The backstack is popped until Fragment A but Fragment C is overlaid with Fragment A, both are visible at the same time. Is this normal behavior or is it me who makes a mistake?
Here's a remark also: all the fragments have transparent background.
Thanks!
This happens because the top fragment (in this case Fragment C) is not removed. You have to remove it first inside a fragment transaction. Try this:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment topFragment = fragmentManager.findFragmentById(R.id.fragment_container);
if (topFragment != null) {
fragmentTransaction.remove(topFragment);
}
fragmentTransaction.commit();
fragmentManager.popBackStack("backstack_state", 0);