Android FragmentTransaction with fragment overlay - android

I have a actionbar tab setup with fragments for each of the pages. (extended default example from wizard).
I'm attempting to create a new fragment and make it the current fragment, with the other hidden
ConfigDetailSectionFragment configDetailSectionFragment = new ConfigDetailSectionFragment();
FragmentManager fragmentManager = getFragmentManager();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_main_config, configDetailSectionFragment);
transaction.addToBackStack(null);
transaction.commit();
the code shows the new fragment but seems it is transparent and I can see the fragment below.
i'm probably forgetting to do something... what am i forgetting to do or am missing?

Adding an android:background="#FFFFFF" fixed the issue, per beworker. Thanks!

Related

cannot use `BottomNavigationView` and `ViewPager` and `TabLayout` simultaneously

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?

"Kill" fragment after replace it

I have a MainActivity with two tabs (using viewPager). In the second tab I have a mapFragment - Google Maps API. After clicking in a button it would open another fragment. However, the mapFragment is under the fragment (in log I see that the map is re-drawn).
Any suggestion how to solve it?
If you are using add() to go to the new fragment, replace it with replace().
See this example:
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container, new NewFragment(), "NewFragment");
ft.commit();

Android Change main Fragment from child

I'm having some troubles with my Fragments management..
It's quite new for me and I'm pretty sure you'll find an easy way to solve my problem.
So I have a NavigationDrawer in a FragmentActivity and from this, I can navigate between two fragments.
One of those two fragments has children in a ViewPager.
I'd like to change the Parent Fragment from a ChildFragment which is in the ViewPager..
With my friend Paint, I drew my problem
http://goo.gl/OGdf1c
So as you can see, I'd like to load FragmentA from Fragment2B
Thank you !
Just perform a normal FragmentTransaction and be sure to use the FragmentManager of the Activity!
FragmentManager manager = getActivity().getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(...);
transaction.commit();
If your Activity is a FragmentActivity or ActionBarActivity you have to remember to use the support FragmentManager, aside from that everything stays the same:
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(...);
transaction.commit();

Create/destroy or show/hide Fragments?

I have a situation in which I need to show/hide a fragment based on a dynamic variable. My question is should I create the fragment once, and then show/hide based on the variable. Or should I destroy/create it each time?
Create the fragment
Fragment alertFragment = new AlertFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(android.R.id.content, alertFragment).commit();
After this should I call show/hide each time?
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.show(alertFragment) // or hide
.commit();
Seems like a lot of work to show/hide something each time. Is this the right way to do it?
I'd say that depends on weather it matters for your application if your Fragment is destroyed or not.
If it doesn't matter at all, simply replace the Fragment everytime by a new one. This is the simplest solution and does not require any logic.
Fragment f = new Fragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content, f).commit();

Android: Merging two fragments

I am new to Android Development. I am using single FragmentActivity and it contains FrameLayout. I use many fragment classes.Some times when I replace one fragment to another their layouts are merge with each other. How can I solve this problem?
Are you doing the following ??
ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.addToBackStack(null);
ft.commit();
Where ft is your FragmentTransaction, and fragmentManager is your
FragmentManager fragmentManager = getSupportFragmentManager();
use replace to add new fragment
ft.replace(id,newfragment);
and remove the old fragment
ft.remove(old fragment);

Categories

Resources