Mario
Mario

Reputation: 145

Material container transform transition not working on back press

I read about the new motion transition added to material design library here https://material.io/develop/android/theming/motion. and wanted to implement the same for my app.

My App is Ecommerce app to sell products. So I wanted to implement container transform transition when I navigate to product details by clicking on product in recycler view. I went through many tutorials which were mostly in kotlin and then used in my java code and now transition from product list to details works but when I click back it doesn't show transition like shrink to recycler item. Can't figure out what I am missing. Also FYI, I am using navigation architecture component for the app.

Here is my code what I implemented

ProductListFragment.java

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setExitTransition(new Hold());
    setReenterTransition(new Hold());
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

    postponeEnterTransition();
    ViewGroup viewGroup = (ViewGroup) view.getParent();
    viewGroup
            .getViewTreeObserver()
            .addOnPreDrawListener(() -> {
                startPostponedEnterTransition();
                return true;
            });
    super.onViewCreated(view, savedInstanceState);
}

ProductAdapter.java

public void onProductClick(View view) {
        FragmentNavigator.Extras.Builder extras = new FragmentNavigator.Extras.Builder();
        binding.container.setTransitionName(products.get(position).getProductId());
        extras.addSharedElement(binding.container,binding.container.getTransitionName());
        FragmentNavigator.Extras build = extras.build();

        NavController navController = Navigation.findNavController(activity, R.id.nav_host_fragment);
        navController.navigate(ProductListDirections.actionNavHomeToNavProductDetails(products.get(position).getProductId()),build);
    }

ProductDetails.java

 @Override
public void onCreate(@Nullable Bundle savedInstanceState) {

    MaterialContainerTransform transform = new MaterialContainerTransform();
    transform.setDuration(300);
    transform.setFadeMode(MaterialContainerTransform.FADE_MODE_CROSS);
    transform.setElevationShadowEnabled(true);
    transform.setScrimColor(Color.TRANSPARENT);
    transform.setContainerColor(Color.WHITE);
    //transform.setAllContainerColors(R.attr.color);
    postponeEnterTransition();
    setSharedElementEnterTransition(transform);
    super.onCreate(savedInstanceState);
}

 @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    binding.detailsContainer.setTransitionName(ProductDetailsFragmentArgs.fromBundle(getArguments()).getProductId());

}

 @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
 mViewModel.observeProductDetails().observe(getViewLifecycleOwner(), new Observer<ProductDetails>() {
        @Override
        public void onChanged(ProductDetails productDetails) {
           
            startPostponedEnterTransition();
        }
    });
}

Am I missing anything? Plz help me on this.

Upvotes: 0

Views: 984

Answers (1)

Fred Waltman
Fred Waltman

Reputation: 9

I am still very much a grasshopper when it comes to Android Transformations but I did come across this post by Chris Banes that says if you use a FragmentManager you need to add

.setReorderingAllowed(true)

at the start of your Fragment transaction if you are postponing the transition.

Hope this steers you in the right direction...

Upvotes: -1

Related Questions