5

I open from ActivityA ActivityB with shared element transition. It's work properly. When I press "Back" button, shared element similarly work properly and I see close animation.

But if I press the "Home" button and after that I return to the application (ActivityB), and after that I click "Back" to go to ActivityA, the transition does not work.

Same situation with three activity.

ActivityA -(work)-> ActivityB -(work)-> ActivityC

after that I start to press the "Back" button

ActivityC -(work)-> ActivityB -(DONT WORK)-> ActivityA

work - it's mean shared element transition work and i see animation

For start Activity I use

ActivityOptionsCompat options = ActivityOptionsCompat
                        .makeSceneTransitionAnimation(getActivity(), itemView, transitionName);
startActivity(intent, options.toBundle());

Try change Manifest (DONT WORK)

android:name=".ActivityB"
android:launchMode="singleTask"
android:parentActivityName=".ActivityA"

Try save shared elements (DONT WORK)

setEnterSharedElementCallback(new SharedElementCallback() {
            @Override
            public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
                if (names.size() > 0)
                    mSharedElementsNames.addAll(names);
                else
                    names.addAll(mSharedElementsNames);

                if (sharedElements.size() > 0)
                    mSharedElementsMap.putAll(sharedElements);
                else
                    sharedElements.putAll(mSharedElementsMap);
            }
        });

I noticed that after minimizing the application, the onMapSharedElements() is not called when press "Back". Otherwise the call is happening.

I guess onStop() call break the shared element transition. But i can't avoid this. Any help or ideas, please

vanilaboy
  • 51
  • 2

1 Answers1

0

I solved this problem by follow this answer.

Simply, just add below snippet code to onStop of ActivityB

override fun onStop() {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q && !isFinishing) {
        Instrumentation().callActivityOnSaveInstanceState(this, Bundle())
    }
    super.onStop()
}
Robust
  • 2,415
  • 2
  • 22
  • 37