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