FragmentTransaction animation - show enter fragment above exit fragment - java

When you use are examined animation shift fragments (outgoing fragment has a longer duration than the animation comes fragment) faced with the fact that the fragment comes time animation falls under the outgoing fragment - I expected it to be animated over the outgoing fragment. Who knows how to achieve the desired effect it?
first fragment add:
fragmentManager.beginTransaction().add(R.id.frame_layout_fragments_container, new FragmentHome(), "home").commit();
replace fragment:
fragmentManager.beginTransaction().setCustomAnimations(R.anim.forward_show, R.anim.forward_hide, R.anim.back_show, R.anim.back_hide).replace(R.id.frame_layout_fragments_container, fragment, fragmentName).commit();
simple illustrations
now I have:
what I need:

something like Zoom-out page transformer?? http://developer.android.com/training/animation/screen-slide.html

I think you could try the following approach as a work around.
fragmentTr.setCustomAnimations(enter, exit);
fragmentTr.hide(currentFragment);
fragmentTr.add(containerId, newFragment, "aTag");
fragmentTr.show(newFragment);
fragmentTr.commit();
Do not use replace(). I tried the above approach, and it worked.
Hope this helps.

ViewCompat.setTranslation(getView, 100.f);
look for https://stackoverflow.com/a/33816251/6642400

Related

Previous fragment is restarting when we go back to previous fragment using backstack in android

lets say we have two fragment and we are in second fragment then if someone press back button then we successfully go back to the previous fragment (i.e. first fragment) using backstack but the problem is that the previous fragment is restarting everything. I want to display content of previous fragment without any restarting or initializing when we press back button.Thanks in advance.
Welcome to Android Fragment Life !!
As suggested by #Uuu Uuu, you need to use add() method while adding fragment. You are getting the fragment overlapped because there is a new fragment added each time.
You simply need to do a check if the fragment exits already then there is no need to add a new fragment. You can assign a 'tag' when adding fragment. Code as follows-
if (fragmentManager.findFragmentByTag("First Fragment") == null)
fragmentManager.beginTransaction().add(R.id.fragment, new FirstFragment(), "First Fragment").commit();
If you are new to android development, please learn about the fragment/activity life cycle, there is a wonderful article By Jose Alcérreca
I hope this will help, happy coding.

Switch between fragments from inside one of the fragmens

Two days ago i asked the following question:
Change the fragment in a framelayout from within another fragment of said framelayout
A fellow user Krish, really helped me out in finding out what was wrong with the way i thought, but i am stil not sure how to actually get what i want done.
I want to be able to switch between three fragments in one FrameLayout.
- the first of the three is loaded at the start of the parent fragment and when back is pressed at the second fragment
- the second must be replacing the first at the click of an item in the listview of the first fragment, and when the back button is pressed from the third fragment
-the third must be loaded when a button is pressed in the second layout
I've tried achieving this by calling the following line whenever the fragment must be changed. A1_frame is the FrameLayout of the parent Fragment/Layout and A1_B0_C2 is the fragment i am replacing
getChildFragmentManager().beginTransaction().replace(R.id.A1_Frame, new A1_B0_C2()).addToBackStack(null).commit();
From what i understand the problem with my solution is that it isn't possible to replace a fragment in the FrameLayout of a parent Fragment/Layout, but if it would work, it would solve my solution. thats why i chose to put it in here.
I hope someone is able to tell me what would work!
getChildFragmentManager() returns the Fragment manager of the Fragment it's being called from, in this case whatever Fragment is in A1_Frame
The method you're looking for is getFragmentManager(), which returns the Fragment Manager of the Activity/Fragment that the Fragment is a part of. I.e. MainActivity, or whatever is creating your first fragment.

Android: How can I get the layout of a fragment instance by way of tag?

Right now I"m inside a fragment, and I'm adding another fragment to the fragment I currently occupy. I'm doing it like this:
fragmentTransaction.add(R.id.templateFragmentLayout, frag2, fragString);
But the fragment I'm nested in is itself added multiple times, so I could be in any one(there are tags on those, if that helps). What I figured would happen is my new frag, frag2, would get added to my current parent fragment at R.id.templateFragmentLayout. But what is happening is when my parent frag has multiple instances, frag2 only ever gets added to the original R.id.templateFragmentLayout, not to any of the corresponding new instances.
So what I"m wondering is, can I somehow specify which fragment to add to? Something like this
fragmentTransaction.add((R.id.templateFragmentLayout of my current fragment), frag2, fragString);
or
fragmentTransaction.add((R.id.templateFragmentLayout of tagged fragment "x"), frag2, fragString);
This is my first question on here, so please let me know if there's anything I should add, I just didn't want to clutter it up with a super long post if there's something simple I"m missing.
fragmentTransaction.add(R.id.templateFragmentLayout, frag2, fragString);
Fragment fragment_byTag = getSupportFragmentManager().findFragmentByTag("fragString");
OK, so in case anyone comes across this with the same issue, I figured it out. In my child fragment, I just had to call getChildFragmentManager() instead of getFragmentManager().

Adding tablet support: Fragments and Activities for Master/Detail (Android)

I'm implementing a fragment hierarchy similar to the one described in Fragments (Android Developers).
In adition in tablets in portrait the app should behave as in the second case. My problem is to handle the transaction from an orientation to other.
The first idea I considered was:
From landscape-to-portrait:When the activity A goes to portrait: Remove the fragment B to the view. Start activity B for result passing the propper values for recover the original fragment B state
From portrait-to-landscape: When the activity B goes to portrait. finishes (with the fragment 2 status in the result). The activity A with the result restores and adds the fragment B to its layout.
But this solution is pretty complex and I think it provably is not a nice idea. The alternative solution I have considered is only to have an Activity. That activity layout is:
<FrameLayout>
<LinearLayout>
<Fragment A>
<Fragment B>
<Slot>
For small devices:
The app removes the fragment B and when a item is selected add to the backstack the fragment to the "Slot"
For tablets:
Using fragmentTransactions the fragment B is moved from Its position to the "Slot" using the backstack to behave properly to the orientation changes
I think the second option sounds better but, is the correct way of doing this?
Thanks
If you want my advice, I'd say it depends on way too much factors. I think you should stick with what you find manageable enough. It depends too on how complex your app's screen flow is.
Keeping it in one activity is, for me, a good idea if you don't have that much fragments to manage. An advantage of this approach is that you don't need to fiddle around with the life cycle of two different activities.
Anyway, finding the implementation complex is in a way an indicator that what you're planning won't be manageable for you in the future.
Hope that helps!
I dont get why would you want to do it in such a complicated way. Have one activity on tablets, two activites on phone. Have first activity implement a listener that would fire if list fragment's item was clicked. The activity knows if it is inside single or dual pane mode, so inside that onItemSelected callback method, have it either start a new activity in case of a single pane mode, or replace a fragment, in case of a tablet.
You can also see this, using Master/Detail template when creating a new project.

Removing a fragment in another fragment activity

I have a fragment that implements an onClickListener, and then the onClickListener tries to remove a fragment.
I get a red line under badFragment in transaction.remove(badFragment);. My best guess is because it can't tell what that fragment is/where it is.
How would I go about finding that fragment, so that it can be removed?
I've tried findFragmentId(badFragment); but it doesn't work.
Bonus points if you can let me know where you found this method. As I'm not great at looking up things in the Android Docs.
You can find fragment
getFragmentManager().findFragmentById(R.id.fragment_id);
or
getFragmentManager().findFragmentByTag("tag");
you can remove fragment itself
getActivity().getFragmentManager().beginTransaction().remove(this).commit();

Categories