Hide previous Fragment when adding new one - android

I have 2 Fragment's - A and B.
In order to switch from Fragment A to Fragment B I'm using this function:
public static void swapFragments(FragmentManager fragmentManager, int containerViewId, Fragment newFragment) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(containerViewId, newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Let's say Fragment A takes the entire screen's area and Fragment B takes only the upper half of the screen.
The problem: When switching to Fragment B, the user is still able to see Fragment A at the lower half of the screen...
How can I hide Fragment A when switching to Fragment B ?
p.s: I don't want to use replace instead of add in the swap function above - I don't want Fragment A's onCreate() to be called each time the user navigate back from Fragment B to Fragment A...
Thanks in advance.

Could you maybe move Fragment A? E.g. slide it down when Fragment B appears? Simply move it outside the screen. Something like what's discussed in this question

Well, without any other option, I've just took care that Fragment B will occupy the entire screen so that Fragment A can't be seen.
More adjustments should have n=been done due to Android's bugs, such as: Google's stupid bug

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 Fragment - add and bring to front

The thing I'm trying to achieve should be quite simple but I can't manage to find the solution to my problem.
I have fragment A. When fragment A's button is clicked, fragment B is displayed:
#Override
public void onClick(View v) {
Fragment fragment = new PostMeasurementFragment();
FragmentTransaction fragmentTransaction = ((PostActivity)context).getSupportFragmentManager().beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.add(R.id.post_container, fragment, "PostMeasurementFragment");
fragmentTransaction.commit();
}
Fragment B has a transparent background so I'd like fragment A to be visible behind fragment B. If I use .replace rather than .add, I can see only fragment B and that's make sense because fragment A was removed by the .replace method. When I use .add I can't see fragment B though and I know it's there because of .addToBackStack: I need to click the back button twice to get rid of fragment A, which makes me thing the first click is quitting fragment B which I can't see.
Given that, I believe fragment B is being added behind (or below, if you think of it as a pile) of fragment A. Is there a way to bring it to front?
Inflate Fragment B into a view in the layout of Fragment A (such as a FrameLayout) where the height that view is set to wrap_content, and the Fragment's view will fill whatever size necessary. Use add() when building the FragmentTransaction.

Need the old fragment on top, when using backstack

Main screen has fragment A.
Clicking replaces fragment A with fragment B.
Pressing the back button replaces fragment B with A.
However until the full animation is completed fragment B is on top of fragment A.
I want them the other way around.
The obvious code:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_down, R.anim.custom_out, R.anim.custom_in, 0);
fragmentTransaction.replace(R.id.content, fragment, "contentFragment").addToBackStack(null).commit();
The effect i'm trying to achieve:
Imagine fragment A is a door.
The door opens (fragment A exit animation) and fragment B falls in the screen (fragment B enter animation)
When you press the back button, fragment B remains as is and the door (fragment A) closes on top of it (fragment a enter animation)
Anyone knows how to do this in a clean way, without adding another fragment A when the back button is pressed?
So far the best way was to have 2 fragment containers one on top of the other.
I keep fragment A in top container and fragment B in other one
and in 1 transaction i do remove / add on the 2 different containers.

How to keep state of Parent Fragment

I have a Fragment, in which I have a nested fragment. I've attached an image to illustrate. So I have child a nested, when I click button 1, I replace child A with child B, then on button 2 click I replace child B with child C. Now when I click on button 3, I replace the parent (Fragment 1 with Fragment 2), this is what I want to do.
When I hit the back button when on Fragment 2, I pop the backstack and I display fragment 1, the problem is child A is displayed, I need to figure out how to display child c when I go from Fragment 2 to Fragment 1.
I need to mention also that child c contains test results that are displayed in a grid view. Can someone help me do this please?
EDIT
Below is the code I'm using for the transactions for the child fragments (button 1 and button 2 click)
protected void nextNestedFragment(Fragment nestedFragment){
FragmentTransaction ft = getParentFragment().getChildFragmentManager().beginTransaction();
ft.setCustomAnimations(R.animator.enter_slide_in,R.animator.enter_slide_out,R.animator.close_slide_in, R.animator.close_slide_out);
ft.replace(R.id.nested_fragment_container, nestedFragment).addToBackStack(null).commit();
}
So for the above I pass in the next fragment I wish to navigate to within the parent fragment. Below is the code I use on button 3 press to navigate from Fragment 1 to Fragment 2:
protected void nextFragment(Fragment nextFrag){
FragmentTransaction ft = getParentFragment().getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.animator.enter_slide_in,R.animator.enter_slide_out,R.animator.close_slide_in, R.animator.close_slide_out);
ft.replace(R.id.fragment_container, nextFrag).addToBackStack(null).commit();
}
First, you need to know, that you have 2 different FragmentManager here (the default one, and ChildFragmentManager) and each with it's own back stack. And when you press a back button, you pop a back stack of your first FragmentManager, which shows you Fragment 1.
Second, when Fragment 1 is being popped from back stack onCreateView of that fragment is invoked. And I'm pretty sure you are creating a view there with a child A inside.
What you need to do is to save Fragment's state. There are a lot of questions here on how to do this correctly. Start from here.

Android fragmentTransaction add() and fragment's onPause

I've implemented navigation in my Android app as one Activity and many Fragments.
When I open new child Fragment, I call this:
FragmentTransaction tx = getSupportFragmentManager().beginTransaction() .addToBackStack(null).replace(R.id.fragment_container, f)
.setBreadCrumbTitle(f.getClass().getSimpleName());
tx.commit()
This works quite well, as old fragment isn't destroyed, so when I go back in the back stack, the old fragment retains the scroll position and everything entered on it.
The problem is, in one Fragment I actually need to pause something when I add another fragment above it. I've thought that onPause() will be called, but it doesn't - I suppose that all other fragments live beneath the ones on top of them.
On the side note, I also think that my app gradually slows down as the user navigates deeper. Shouldn't I be replacing the fragment in the transaction instead, so only one is on the top?

Categories

Resources