Android animation between activity and fragment - android

I got a trouble when I do an animation between an activity and a fragment the animation for the activity is not working only the fragment move.
FragmentTransaction transaction = activity.getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.animator.slide_in_from_left, R.animator.slide_in_from_right);
transaction.replace(R.id.main_content, fragment);
transaction.addToBackStack(null);
transaction.commit();
The activity should go on the right, but the activity don't move.
I try this code for switching one fragment to another fragment and the previous fragment move on the right all works correctly.
Why when I use this on activity he doesn't move ?

Related

Fragment popBackStack without animation

On navigating from one fragment to another which are part of same activity, I am using slide animation.
WebFragment fragment = WebFragment.newInstance(Globals.TGURL_CREATE_ACTIVITY, "");
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.setCustomAnimations(R.anim.enter_anim, R.anim.exit_anim, R.anim.enter_anim, R.anim.exit_anim);
transaction.replace(R.id.fragment_activity_layout, fragment);
transaction.addToBackStack(null);
transaction.commit();
This code ensures that when I come back to the first fragment, animation is there.
Well this has been a boon for me so far. But in one specific case it is becoming a curse. The activity is placed on ActionBar tab. When the 2nd (WebFragment) is the current one, and I tap the tab and not the back button, I want the first fragment to be displayed without any animation.
But it has been impossible for me to do this so far as the navigation is inheriting the animation which was earlier given.
This is what I am doing for going back :
TabActivity.this.getSupportFragmentManager().popBackStack();

When fragment calls fragment, who is the parent of second fragment?

So I have started fragment A from my main activity and that fragment contains a button. When that button is clicked this code runs :
Fragment newFragment = new HomeFragment();
// consider using Java coding conventions (upper first char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.formFragment_Container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
So, as you can tell, I fire up fragment B from fragment A and what I don't get is what activity hosts that fragment B, since there is no indication of main activity in this code above, nor there is indication of fragment B in main activity. Could you clarify this for me. Thanks !
When you call to
getFragmentManager()
It use instance of FragmentManager that the Activity use to manage it Fragments. So the Activity will be the same as Activity A.
It comes under the concept of nested fragments in Android. For both Fragment A and B, its associated activity is main activity(from which you have added Fragment A).
You can access Main activity and Fragment A from Fragment B.
For example. getActivity() will return Main Activity, getParentFragment() will return Fragment A.

back stack and statically added fragment

I have a fragment added statically from XML I want to replace this fragment by another fragment, I did that by adding this code:
CFragment singleStationFragment = new CFragment();
android.support.v4.app.FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.layoutlist, singleStationFragment);
transaction.addToBackStack(null);
transaction.commit();
the problem is that when I press the back button the first fragment is not shown because it was not added through a transaction and the manager doesn't know about it, is there a way I could add the first fragment (ALREADY ADDED FROM XML), to my backstack or I could just show it when I click back instead of exiting the app ? Thanks !
As far as I'm aware you will have to add your first fragment to the activity in code rather than in the layout file. Do this with the add method of FragmentTransaction
transaction.add(R.id.FragmentContainer, fragment);

Android fragment unexpectedly close

I use a holder activity with FrameLayout.
There I put a fragment with a listview. It works fine.
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragments, feedFragment);
ft.commit();
Then I add another fragment.
android.support.v4.app.Fragment targetFragment = new MainPhotoFragment();
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragments, targetFragment);
ft.addToBackStack(null);
ft.commit();
Here I use add() instead of replace() to return to previous position of the listview when hitting back key. It works fine.
But it is possible to navigate to the third fragment from the second fragment.
android.support.v4.app.Fragment targetFragment = new FullPhotoFragment();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragments, targetFragment);
ft.addToBackStack(null);
ft.commit();
Here I use replace to force the 2nd fragment to reload when hitting back key.
Sometimes back key from the third fragment works fine, it displays the second fragment that is reloading on appearing.
But sometimes (as I can see it happens first time when I try this steps) hitting back key from the third fragment leads me to the first fragment, closing the second fragment against my expectations. And the first fragment is reloading.
How to prevent this strange behavious?
add() method will add Fragments to Container and any other fragments added to the Container will be queued back of the first fragment. They will be not visible until and unless first fragment made Invisible. I hope this is the problem you are facing. It would be good if you use replace() for the first-->second fragment navigation also.

Manually adding Fragment to BackStack

I'm currently using a ViewGroup (SwipeyTab implementation) to switch between Fragments. However, some Fragment "pages" get replaced by other Fragments on the same Tab, so I initially tried:
FragmentTransaction ft = fragment.getFragmentManager().beginTransaction();
ft.remove(currentFragment);
ft.add(newFragment,"");
ft.commit();
That code would remove the current fragment but not add newFragment (from Logcat, it would get instantiated but not appear).
I ended up adding it in the FragmentPagerAdapter.getItem(int position) call based on current state (based on this: Replace Fragment inside a ViewPager). However, I'd like to be able to add each newly replaced fragment to be part of the back stack.
I tried adding to backstack before removing the fragment:
currentFragment.getFragmentManager().beginTransaction().addToBackStack(null).commit();
FragmentTransaction ft = fragment.getFragmentManager().beginTransaction();
ft.remove(currentFragment);
ft.commit();
and that didn't work - it added the last fragment to the backstack, so when I pressed back, it would just reload the current fragment.
Is there anyway I can add a fragment to the backstack that has been replaced in the "non traditional" way?

Categories

Resources