Moving from fragment to fragment by new activity
I am working on an application that is currently using moving content to ImageView
from one chunk to another with the same activity. It works fine, but I figured out that my target fragment should have its own activity.
So let's say I have an Activity A that contains Fragment 1 And I have an Activity B that contains Fragment 2 . I need to transition a common element from Fragment 1 to Fragment 2 .
Here's what I have done so far: In the callback method from fragment 1 to activity A, I pass the selected object as well as the image I want to navigate from.
Activity A
@Override
public void OnPhotographSelected(Photograph selectedPhoto,ImageView image) {
Intent i= new Intent(this, PhotoDetailActivity.class);
i.putExtra("photo_OBJ", selectedPhoto);
i.putExtra("transitionName", image.getTransitionName());
startActivity(i, ActivityOptions.makeSceneTransitionAnimation(this, image, "mainPhoto").toBundle());
}
Activity B
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_detail);
Photograph photoObj=new Photograph();
Bundle b = getIntent().getExtras();
String transitionName="";
if(b!=null)
{
photoObj=(Photograph)b.getSerializable("photo_OBJ");
transitionName=b.getString("transitionName");
}
PhotoDetailFragment pdf = PhotoDetailFragment.newInstance(photoObj);
pdf.setSharedElementReturnTransition(TransitionInflater.from(this).inflateTransition(R.transition.change_image_transform));
pdf.setSharedElementEnterTransition(TransitionInflater.from(this).inflateTransition(R.transition.change_image_transform));
pdf.setImageTransitionId(transitionName);
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.photo_detail_content, pdf);
trans.commit();
}
Fragment 2
mainImg.setTransitionName(mImageTransitionID);
Theme of the event
<item name="android:windowActivityTransitions">true</item>
I don't see the content transition at runtime. As I mentioned earlier, this transition worked correctly from fragment to fragment within the same activity. It's also worth noting that Fragment 1 is a gridview, so I have to maintain the transitionNames myself, so they are unique, which is why you see calls to setTransitionName at runtime.
Any idea why I am not seeing the transition?
source to share
Try using postponeEnterTransition()
in second activity inside onCreate()
and yourActivity.startPostponedEnterTransition()
in your fragment after creating your view in onViewCreated()
.
If you are using AppCompat try supportPostponeEnterTransition()
both supportStartPostponedEnterTransition()
or ActivityCompat.postponeEnterTransition(yourActivity)
and ActivityCompat.startPostponedEnterTransition(yourActivity)
.
Credits to: http://www.androiddesignpatterns.com/2015/03/activity-postponed-shared-element-transitions-part3b.html
source to share