简体   繁体   中英

finishAfterTransition() not working properly

In my app I want the passage from Activity A to Activity B to be animated. In particular, I want the Activity A to slide out and Activity B to fade in when clicking on a button, and, when clicking the back button, Activity B to fade out and Activity A to slide in. Well, the first animation (slide out - fade in) works fine. But when I click the back button, sometimes the animation triggers, but most of the times I see the fade out of Activity B and the blank screen, Activity A doesn't appear at all. What could be wrong? Here's my code:

Activity A:

      @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setupWindowAnimations();
        }
...
}
@Override
protected void setupWindowAnimations() {

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
    Slide slide = new Slide();
    slide.setDuration(1000);
    slide.setSlideEdge(Gravity.RIGHT);
    getWindow().setExitTransition(slide);
    getWindow().setReenterTransition(slide);
}

}

Activity B:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setLayout(R.layout.file_browser);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setupWindowAnimations();
    }
 ...}
 @Override
protected void setupWindowAnimations() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        Fade fade = new Fade();
        fade.setDuration(1000);
        getWindow().setEnterTransition(fade);
        getWindow().setReturnTransition(fade);

    }
}

Clicking on the button which starts Activity B from Activity A:

   fab.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onClick(View v) {
            ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(ActivityA.this);
            Intent intent = new Intent(ActivityA.this,ActivityB.class);
            startActivity(intent, options.toBundle());

        }});

The method for the back button:

 @Override
public boolean onSupportNavigateUp(){

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        finishAfterTransition();

    }
    return true;

}

I'm probably missing something. I hope you'll help me to figure it out. Thanks in advance!

put getWindow().setAllowEnterTransitionOverlap(false); in activity B like this:

protected void setupWindowAnimations() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        Fade fade = new Fade();
        fade.setDuration(1000);
        getWindow().setEnterTransition(fade);
        getWindow().setReturnTransition(fade);
        getWindow().setAllowEnterTransitionOverlap(false);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM