按下电源按钮后,Android清除“活动到活动"共享元素过渡退出动画 [英] Android clears Activity to Activity shared element transition exit animation after pressing power button

查看:91
本文介绍了按下电源按钮后,Android清除“活动到活动"共享元素过渡退出动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过从 RecyclerView GridLayoutManager 到全屏详细信息的共享元素来实现基本的 Activity 过渡动画.活动屏幕.动画在常规情况下效果很好.因此,当单击网格中的图像时,它会缩放为全屏图像,并且在退出时会发生相反的情况.但是,如果在显示详细信息屏幕时按电源按钮并返回到应用程序,则Android似乎会清除所有已注册的共享元素/转换,因此全屏图像而不是缩小为网格只会淡出.我尝试在两个 Activity 中注册 SharedElementCallbacks ,在没有按下电源按钮的情况下可以正确调用它们,但是在按下电源按钮后都不会被调用.非常感谢您提供帮助解决此问题的建议.

I'm working on implementing a basic Activity transition animation with a shared element from a RecyclerView with a GridLayoutManager to a full screen details Activity screen. The animation works well under regular circumstances. So when clicking on an image in the grid it scales to the full screen image and on exiting the reverse happens. But if you press the power button and return to the app while the details screen is visible, Android seems to clear all registered shared element/transitions so the full screen image instead of scaling back into the grid it just fades out. I tried registering SharedElementCallbacks in both Activities which are called properly without the power button press but neither gets called after pressing the power button. I'd appreciate any suggestions to help solve this problem.

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

These are the places that I've added code to support the shared element transition:

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

Well, I believe this is a bug starting from API 29 in

  1. 活动2-活动
  2. 活动片段
  3. 片段活动

共享元素转换.在API<中可以正常工作29和Fragment-Fragment过渡.

shared element transitions. It works fine in API < 29 and Fragment-Fragment transitions.

ActivityA 过渡到 ActivityB 并从 ActivityB 返回时

我找到了解决此问题的简单方法,这使我们过渡到无法正常工作-

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

ActivityB 发送到后台时,它会调用 onPause()>活动的onStop() 生命周期方法,现在位于您的内部 onStop() ,您可以检查以下情况,如果活动未完成,并且您的API>29,然后在 callActivityOnSaveInstanceState()

While sending the ActivityB to background it calls the onPause() > onStop() lifecycle methods of the activity and now inside your onStop() you can check for the following conditions, if the activity is not finished and your API > 29, then pass the current Bundle in callActivityOnSaveInstanceState(),

科特林代码//在您的ActivityB中覆盖此方法

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代码//在ActivityB中覆盖此生命周期方法

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();
    }

希望对您有帮助!

这篇关于按下电源按钮后,Android清除“活动到活动"共享元素过渡退出动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆