Slide right to left transition anim. Fragment -> Activity - android

I have a fragment with list view on it. When the user clicks on any of the list view's items he/she is transferred in an Activity. The default animation of the activity that is started (startActivity from the fragment -> opens this activity) is from the bottom slide to top of the screen. How I can override this animation and create a slide from right animation when the activity is created.
I have tried to override the overridePendingTransition method in the activity but that did not change anything.
#Override
public void overridePendingTransition(int enterAnim, int exitAnim) {
super.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
}

From the Activity.overridePendingTransition documentation :
Call immediately after one of the flavors of startActivity(Intent) or finish() to specify an explicit transition animation to perform next.
In other words, call the method on the current Activity after starting the new Activity, instead of overriding it.

Related

Shared Element Transitions not working when exit from fragment to Activity containing Viewpager

I have a RecyclerView inside the fragment in a Viewpager and sharing element from an item in the RecyclerView to a DetailActivity containing a fragment. I am setting transition inside the DetailActivity for the entry and exit of the fragment.
public static void sharedTransitionReceiver(Activity activity, Fragment fragment) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().getEnterTransition().setDuration(500);
Slide slideTransition = new Slide(Gravity.START);
slideTransition.setDuration(500);
fragment.setReenterTransition(slideTransition);
fragment.setExitTransition(slideTransition);
fragment.setSharedElementEnterTransition(new ChangeBounds());
}
}
It is working just fine but when I navigate back to the previous activity containing the Viewpager exit animations are not working and item won't animates back to their original position.
After 4 hours of effort, I cannot imagine the answer to be that simple. I was calling finish() after popping out every fragment from stack instead I have to use supportFinishAfterTransition();.

Catch the end of Return Activity Transition

From Activity A to B , I set enter and return transitions.
I want to put a fadein animation after the return transition ( B to A after a back).
Originally , I thought that in Activity's onStart method will a good place.
It turns out I don't see the fadein animation so I suppose the ending of the Return Transition happens after onStart()
I checked with SharedElementCallback but it doesn't tell when the transition ends.
How to catch the moment when the Return Transition ends ?
For Activity you'll need to call overridePendingTransition() in Activity A after calling startActivity() and provide your own animations. You can also override onEnterAnimationComplete() in Activity A so you know when it has been animated on to the screen.
For Fragments, you can do this by adding your own custom animations for the transitions using setCustomAnimations() on the transaction. These will override the default animations for your Fragment transitions.

Animate views before activity finished

I use view animations in a predrawlistener to animate the activity start.
Exist a similar listener to animate my views before my activity is finished?
I know the overridependingtransition method for animations in xml but i want to use ViewPropertyAnimations in a programmatically way for specific views.
I suggest you to override finish() method, then add your animation there:
#Override
public void finish (){
// do your animation
super.finish();
}
You can call YourActivity.finsih(); when you want to finish your activity from code.

Changing reenter animation to another item of a list

I have a RecyclerView with images and when I press an image the app opens another activity that contains a ViewPager with the same images but in the position of the one I selected.
I've done the transition in Lollipop to share this image between activities using supportPostponeEnterTransition and supportStartPostponedEnterTransition in the called activity to wait until the viewPager is loaded with images to start the transition.
When I enter in the called activity and when I press back the transitions are ok.
The problem I'm facing is if I move to another image in the ViewPager of the called activity, when I press back it animates the image that it was selected at the beginning, not the currently selected one.
I've been able to change the animated image to the one selected in the called activity with this:
#Override
public void onBackPressed() {
View view = ((ImageDetailFragment) adapter.getFragment(viewPager,
viewPager.getCurrentItem())).getTransitionView();
ViewCompat.setTransitionName(view, Constants.TRANSITION_IMAGE);
super.onBackPressed();
}
But it is returning to the same position of the original image in the list of the calling activity.
How can I do it to make the image return to its position in the list of the calling activity?
The first thing to do is to make sure that the views work properly without any Activity transition. That is, when your return from Activity with the ViewPager, the RecyclerView Activity should be showing the View that the ViewPage was showing. When you call the ViewPager activity, use startActivityForResult and use the result to scroll the RecyclerView to the correct position.
Once that is working, the Activity Transition can be made to work. I know that you've given each View in your RecyclerView a different transitionName, right? When you bind the View, call setTransitionName and give it a repeatable name. Typically this is the image URL or cursor row ID or at worst some munged index like "image_" + index.
The next thing you need to do is set the SharedElementCallback for both the calling Activity (exit) and called activity (enter). In each, you're going to need to override the onMapSharedElements callback to remap the shared element.
#Override
public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
// assuming just one shared element, excuse the ugly code
sharedElements.put(names.get(0), mSharedElement);
}
Here, mSharedElement has been set in the onActivityReenter in the calling (RecyclerView) activity and in onCreate and prior to finishAfterTransition (onBackPressed?) in the called (ViewPager) activity.
The onActivityReenter gives new functionality specifically for this case. You can look at the results there before the called Activity completes.

Change activity transition when inside a TabHost

I have successfully changed the transitions between activities using overridePendingTransition().
Unfortunately when I am on a TabActivity and use activities inside each tab. When one of those activities inside the content of the tab, starts another activity, the overridePendingTransition() seems to not work.
I basically have a TabActivity, inside it resides an activity with a ListView. What I'm doing is when the item is clicked, I launch the item details' activity.
This new activity's transition animation is not being overridden with the overridePendingTransition()
I basically do this:
private Activity owner;
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent programActivity = new Intent().setClass(view.getContext(), ProgramActivity.class);
Program program = (Program) parent.getItemAtPosition(position);
programActivity.putExtra("programID", program.getId());
owner.startActivity(programActivity);
owner.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
So, I believe that the pending transition is overridden after I'm trying to override them.
Is there a different place I should do that?
Am I doing some other stupid mistake?
Thanks !
I had a similar problem with calling overridePendingTransition() from within a Fragment, because Fragments do not have overridePendingTransition() method defined.
I solved it using:
getActivity().overridePendingTransition(R.anim.enterAnim, R.anim.exitAnim);
Hope it helps. The Fragment was within a TabHost.
I found out that the problem was because my view was a sub-activity inside a tab.
To correctly override the transitions I've overridden the onPause method on the TabActivity and it now works as expected.
Note: You still have to use the overridePendingTransition() on the listener for your items if your activity is NOT within a tab.

Categories

Resources