Android 在按下电源按钮后清除 Activity 到 Activity 共享元素过渡退出动画

Android clears Activity to Activity shared element transition exit animation after pressing power button

我正在使用共享元素实现基本 Activity 过渡动画,从 RecyclerViewGridLayoutManager 到全屏详细信息 Activity 屏幕。动画在正常情况下运行良好。因此,当单击网格中的图像时,它会缩放到全屏图像并在退出时发生相反的情况。但是,如果在显示详细信息屏幕时按下电源按钮并 return 进入应用程序,Android 似乎会清除所有已注册的共享 element/transitions 因此全屏图像而不是缩小到网格它只是淡出。我尝试在两个 Activities 中注册 SharedElementCallbacks ,它们在没有按下电源按钮的情况下被正确调用,但在按下电源按钮后都没有被调用。如果有任何有助于解决此问题的建议,我将不胜感激。

这些是我添加代码以支持共享元素转换的地方:

public class MyViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.imageview) ImageView imageView;

    private Item item;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        itemView.setTag(this);
        itemView.setOnClickListener(onItemClickListener);
    }

    @Override
    public void onBind(int position) {
        super.onBind(position);
        this.item = list.get(position);

        imageView.setTransitionName(item.getId());
        Glide.with(imageView.getContext().getApplicationContext())
                .load(item.getUrl())
                .centerCrop()
                .apply(RequestOptions.placeholderOf(new ColorDrawable(Color.BLACK)))
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(imageView);
    }

    public Item getItem() {
        return item;
    }
}

public class MyActivity extends AppCompatActivity {

    ...

    public void setUp() {
        ...

        adapter.setOnItemClickListener(view -> {
            MyViewHolder viewHolder = (MyViewHolder)view.getTag();
            View view = viewHolder.imageView;

            Intent intent = new Intent(this, DetailsActivity.class);
            intent.putExtra(Item.TAG, viewHolder.getItem());

            ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
                    this,
                    view,
                    view.getTransitionName());

            startActivity(intent, options.toBundle());

        });

        ...
    }
}


public class DetailsActivity extends AppCompatActivity {

    @BindView(R.id.imageview) ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        supportPostponeEnterTransition();

        Bundle bundle = getIntent().getExtras();
        Item item = (Item) bundle.getSerializable(Item.TAG);
        imageView.setTransitionName(item.getId());

        final RequestListener<Drawable> requestListener = new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                supportStartPostponedEnterTransition();
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                supportStartPostponedEnterTransition();
                return false;
            }
        };

        Glide.with(getApplicationContext())
                .load(item.getUrl())
                .centerCrop()
                .addListener(requestListener)
                .into(imageView);

    }
}

好吧,我相信这是一个从 API 29 in

开始的错误
  1. Activity-2-Activity
  2. Activity-片段
  3. 片段-Activity

共享元素转换。它在 API < 29 和 Fragment-Fragment 转换中工作正常。

ActivityA 过渡到 ActivityB 并从 [= 返回时13=]ActivityA 一切正常,除非你在 ActivityB 并发送你的后台或锁定设备屏幕上的应用程序,然后你来到 ActivityB 打开的应用程序,现在按返回按钮或返回到 ActivityA 会丢失所有共享元素反向转换,即使您已覆盖 sharedElementReturnTransition

I found a simple hack to fix this issue which is making our transitions to not work normally -

在将 ActivityB 发送到后台时,它会调用 activity 的 onPause() > onStop() 生命周期方法,现在在您的 onStop()您可以检查以下条件, 如果 activity 未完成并且您的 API > 29,则将当前 Bundle 传递到 callActivityOnSaveInstanceState(),

Kotlin Code // override this method in your ActivityB

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

Java Code // override this lifecycle method in your ActivityB

 @Override
    protected void onStop() {
        if(Build.VERSION.SDK_INT == Build.VERSION_CODES.Q && !isFinishing()){
            new Instrumentation().callActivityOnSaveInstanceState(this, new Bundle());
        }
        super.onStop();
    }

希望对您有所帮助!