I have two activities when I click the Button of the First activity it should move to right not completely out.At the same time the second activity should move from left to right this activity will occupy the 50% of the screen and rest of the Screen will be the first activity.The button will be in first activity .If I again click the button of the first activity the second activity will move from right to left it disappears and the screen is occupied by first activity. I done using override Pending Transition in android the problem is when I click the button the first activity is slide out and second activity occupies screen
You can't show half of activity at a time there will be only one activity visible to user and for implementing the animation you mentioned above you can use two fragment in a activity.
Use two fragment and use animation while adding and removing them.
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
MyFragment next = getMyFragment();
ft.add(R.id.MyLayout,next);
ft.setCustomAnimations(R.anim.slide_in_right,0);
ft.show(next);
ft.commit();
Related
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.
My app has no XML. It is all made of Java code dynamically. In the app, when in portrait mode, only one screen is visible - a fragment which changes inside a FrameLayout inside a single activity. When rotating, the same single activity gets another FrameLayout (on the right) for displaying two halves of the screen.
The app itself has a user flow of fragments.
If a user rotate to landscape, the menu (ListFragment) is shown in the left half of the screen, and the right half of the screen is empty.
If a user selects one item from the ListFragment (on the left), the right FrameLayout shows another ListFragment or FormFragment (this depends of JSON response). If another list shows in the right FrameLayout, if a user selects one item from that list, now this ListFragment goes in left FrameLayout, and in the right is FormFragment etc.
So it is a "fine dance" of replacing the fragments inside left and right FrameLayouts which is then placed on the backstack. So if a user rotate the screen in some moment, I need a way to preserve that backstack, so the user can go back through a backstack when in landscape.
Also, if a user is in portrait mode, I also need a way to preserve that backstack when he rotates from landscape to portrait.
The problem:
How to retain (keep) the backstack of all the fragments, if a user rotates screen from portrait to landscape, and vice versa, so he can go back like it was no change at all.
Is it possible to achieve this in Android? I've tried with setRetainInstance() but I've also read that it is not the way if you want to work with backstack, and also I'm very new to all this, so if anyone can help me with this problem.
Thanks in advance!
You can use addToBackStack() method for adding fragment transaction to back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.
Code snippet :
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace({your_Fragment_view});
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Don't forget to do the addToBackStack(null) so your previous state will be added to the backstack allowing you to go back with the back button.
Reference link - https://developer.android.com/reference/android/app/FragmentTransaction.html#addToBackStack(java.lang.String)
I have 2 fragments the first contains a button which on clicked opens a fragment with a ListView in it. I have a shared element transition for the button to transition into the new fragment (root layout) but I would also like to have this transition in reverse (the list fragment contracts into the button again).
However currently I detect the list item click and send an event to the Activity which pops the listview fragment off the backstack (popBackStackImmediate()) hence does not show the transition.
Is there a good way to allow back navigation while preserving reverse transition to work as well?
did you try this:
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack();
fm.executePendingTransactions();
PS: use getSupportFragmentManager() if you're using the support library else use getFragmentManager().
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 currently have one Activity which contains a Fragment by injecting it inside a LinearLayout during runtime.
Inside my Activity layout, I have a Button view called nextButton. When I click this button, I want the Activity to switch to the next Fragment but ALSO animate where the current fragment moves off the screen to the left and the new fragment comes in from the right. As if the new fragment is pushing the current fragment out of the way.
Below is a demonstration of what I want.
Shall I store all of the Fragments in an ArrayList<Fragment> and just inject the current index + 1 in to the LinearLayout when the nextButton is clicked? What would be the best way to go about this?
All you need is ViewPager, See one good example here
You need to do selectPage with in your nextButton, here true is for smooth scroll animation.
pager.setCurrentItem( num,true )
Please find Reference link here
You need to create 4 animation for entering (left and right) and exiting animation (left and right) of the fragment.
Each time you replace a fragment from the FragmentTransaction you need to set the setCustomAnimations
sample:
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(inL, inR, outL, outR);
transaction.replace(layout, fragment, tag);
transaction.commit();
where inL and inR are entering animation and outL and outR are exiting animation animation