简体   繁体   English

Android自定义从子级转换为父级Activity

[英]Android custom transition from child to parent Activity

I have 2 Activities . 我有2个Activities ActivityA is the parent Activity of ActivityB : ActivityAActivityAparent Activity ActivityB

My Manifest file: 我的Manifest文件:

<activity
        android:name=".ActivityA"
        android:launchMode="singleTop" />
    <activity
        android:name=".ActivityB"
        android:parentActivityName=".ActivityA"/>

I've changed the transition from ActivityA to ActivityB : 我已经改变了从ActivityAActivityB的过渡:

    final Intent intent = new Intent(this, ActivityB.class);
    startActivity(intent);
    overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

That works fine. 这很好。 But now I also want to change the transition when going back from ActivityB to ActivityA . 但现在我也想从回去的时候改变过渡ActivityBActivityA In my ActivityB I override the method finish() like this: 在我的ActivityB我重写方法finish()如下所示:

@Override
public void finish() {
    super.finish();
    overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
}

The problem is that the finish() method is only called when the hardware back Button is pressed, but not when the "go back arrow" (top left corner of the screen) is pressed. 问题是finish()方法仅在按下硬件后退Button时调用,而不是在按下“返回箭头”(屏幕左上角)时调用。

I also want the custom transition when that Button is pressed. 按下Button时,我也想要自定义transition

在此输入图像描述

Any ideas? 有任何想法吗?

you can do it by: 你可以通过以下方式完成:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                  finish();
                  overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);

        }
        return super.onOptionsItemSelected(item);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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