android shared element transition with fragments doesn't show exit animation

291 Views Asked by At

I have a main activity that handles the static elements, attached to the main activity there is a fragment, this fragment has a tablelayout with a viewpager2, each page shows a recycler view. The shared element transition works well from FRAGMENT A(Tab_1) to FRAGMENT B(ElementDetails), but when I press the back button, the return shared element transition doesn't work.

TAb_1(Fragment A):

public class tab1_anime extends Fragment {
    private RecyclerView recycler_view;
    public static AFSGAdapter adapter;
    private ArrayList<AddElement> a_list = new ArrayList<>();
    private Context context;

    public tab1_anime(){ };

    @Override
    public void onCreate(Bundle savedInstancesState) {
        super.onCreate(savedInstancesState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstancesState) {
        View view = inflater.inflate(R.layout.fragment_tab1_anime, container, false);
        recycler_view = view.findViewById(R.id.recycler_view);
        recycler_view.setHasFixedSize(true);
        context = getActivity();
        LinearLayoutManager layoutManager_anime_crucial = new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false);
        recycler_view.setLayoutManager(layoutManager_anime_crucial);
        recycler_view.setItemAnimator(new DefaultItemAnimator());
        int spanCount = 3; // 3 columns
        int spacing = 10; // 10 for my phone, 35 for the emulator
        boolean includeEdge = true;
        recycler_view.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));
        adapter = new AFSGAdapter(a_list, getContext(), this);
        adapter.setDataSet(a_list);
        recycler_view.setAdapter(adapter);
        recycler_view.scheduleLayoutAnimation();
        adapter.notifyDataSetChanged();
        return view;
    }

    public void openShowElementFragment(int position, View view) {
        AddElement element = a_list.get(position);
        ShowElementFragment showElementFragment = new ShowElementFragment();
        Bundle bundle = new Bundle();
        bundle.putString("transitionName", "transition" + position);
        bundle.putSerializable("element", element);
        showElementFragment.setArguments(bundle);
        ((MainActivity) context).showFragmentWithTransition(this, showElementFragment, "showElementFragment", view, "transition" + position);
    }
}

ShoeElementDetails(Fragment B):

public class ShowElementFragment extends Fragment {
private AddElement elemento;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_show_element, parent, false);
    //showelement_background_image.setTransitionName("example_transition");
    Bundle b = getArguments();
    if (b != null) {
        String transitionName = b.getString("transitionName");
        elemento = (AddElement) b.getSerializable("element");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            showelement_image.setTransitionName(transitionName);
        }
    }

    //postponeEnterTransition();

    Glide.with(this)
            .load(elemento.image_url)
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .skipMemoryCache(false)
            .apply(RequestOptions.bitmapTransform(new BlurTransformation(10, 3)))
            .into(showelement_background_image);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getActivity().getWindow().getSharedElementEnterTransition().setDuration(800);//.setInterpolator(new AccelerateInterpolator());
        getActivity().getWindow().getSharedElementReturnTransition().setDuration(850);//.setInterpolator(new DecelerateInterpolator());
    }

    showelement_backarrow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getActivity().getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragmentHolder, new MainFragment())
                    .commit();
            //startActivity(new Intent(ShowElementActivity.this, AnimeActivity.class));
        }
    });
    return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    postponeEnterTransition();
    OneShotPreDrawListener.add(view, this::startPostponedEnterTransition);
    super.onViewCreated(view, savedInstanceState);
   }

}

Inside the adapter I handle the click of the element to start the transition:

inside adapter:

holder.itemView.setOnClickListener(new View.OnClickListener() {
        @SuppressLint("ClickableViewAccessibility")
        @Override
        public void onClick(View v) {
            fragment.openShowElementFragment(holder.getAdapterPosition(), v.findViewById(R.id.main_image));
        }
    });

the openShowElementFragment called is inside main activity:

public void showFragmentWithTransition(Fragment current, Fragment newFragment, String tag, View sharedView, String sharedElementName) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    // check if the fragment is in back stack
    boolean fragmentPopped = fragmentManager.popBackStackImmediate(tag, 0);
    if (fragmentPopped) {
        // fragment is pop from backStack
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            current.setSharedElementReturnTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
            current.setExitTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));

            newFragment.setSharedElementEnterTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
            newFragment.setEnterTransition(TransitionInflater.from(this).inflateTransition(android.R.transition.no_transition));
        }
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragmentHolder, newFragment, tag);
        fragmentTransaction.addToBackStack(tag);
        fragmentTransaction.addSharedElement(sharedView, sharedElementName);
        fragmentTransaction.commit();
    }
}

It's my first time approaching with fragments.

0

There are 0 best solutions below