Can shared element transitions work for fragments in different containers?
Asked Answered
G

0

8

I want to implement the shared element transitions in my app for Android Lollipop. After I have read the documents, the SO questions and some posts I decided to give it a go, but now I got a problem.

The scenario is I have two fragment containers (for tablets), just like the normal list/detail design pattern.

I want to do the shared element transitions between the list fragment to detail fragment upon a list item is touched. The enter of the detail fragment is OK, but as soon as I press back button, app crashes with NullPointerException in the transition framework code.

Is the scenario supported by shared element transition?

Here is the code that starts the detail fragment:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment fragment = DetailFragment.create((int)id);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    View title = view.findViewById(R.id.item_name);
    title.setTransitionName("title");
    listFragment.setSharedElementReturnTransition(
        TransitionInflater.from(this).inflateTransition(R.transition.change_bounds));
    listFragment.setExitTransition(
        TransitionInflater.from(this).inflateTransition(android.R.transition.explode));

    fragment.setSharedElementEnterTransition(
        TransitionInflater.from(this).inflateTransition(R.transition.change_bounds));
    fragment.setEnterTransition(
        TransitionInflater.from(this).inflateTransition(android.R.transition.explode));
    ft.addSharedElement(title, "title");
}
else {
    ft.setCustomAnimations(R.anim.slide_in_right, 0, 0, R.anim.slide_out_right);
}
ft.replace(R.id.detail_panel, fragment)
   .addToBackStack(null)
   .commit();

Logcat is here:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v4.app.Fragment.getAllowReturnTransitionOverlap()' on a null object reference
        at android.support.v4.app.BackStackRecord.configureTransitions(BackStackRecord.java:1201)
        at android.support.v4.app.BackStackRecord.beginTransition(BackStackRecord.java:1029)
        at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:883)
        at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1541)
        at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:502)
        at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:176)
        ...
Gravante answered 24/12, 2014 at 8:20 Comment(2)
Fragment Transitions do not support transitions between different fragment containers. If you need that, you can get the results you want using regular Transitions. That said, I am surprised to hear that the enter transition was OK. The code you have is showing a replace on a single container. That should work. Is it possible that you are replacing a null fragment? If you add() instead, does it work? If so, that is a bug worth fixing.Schnitzler
@GeorgeMount, Thank you for the information! And yes I was replacing a null fragment. The detail container was blank when nothing selected (thus no fragment attached). When an item from the list container was selected, I tried to show the detail fragment. The result of add() seems to be the same. Oh, the enter transition was not entirely OK, I meant it would run. The actual transition was not fully as expected but I thought it was the transitionName thing. Now I think I have a better understanding on the whole transition. Thank you again!Gravante

© 2022 - 2024 — McMap. All rights reserved.