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.
Related
I have 3 fragments in my app: [a]->[b]->[c]. first fragment a is added, then it is replaced with b and then b is replaced with c. when the user is in fragment b he can go back to fragment a by pressing back button, but when in fragment c, fragments a and b must not be accessible with back button (pressing back button must close all fragments).
I have tried using
getSupportFragmentManager().popBackStackImmediate(name, FragmentManager.POP_BACK_STACK_INCLUSIVE);
but it pops all fragments from the top (current fragment) to the specified fragment which is not good idea. any simple solution?!
Imagine i have a frameLayout in my main activity, and two fragments,
so now first fragment is being displayed, i replaced it with secondFragment and added first one to back stack, now some condition is met in the secondfragment so i want to close the second fragment and open the first one where we left that (basically back from the back stack) without pressing the back button. how do i achieve this ?
you can write this code for removing current fragment from backstack:
getActivity().getSupportFragmentManager().popBackStack();
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.
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.
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