Home » Android » android – Shared element activity transition. View stays on the screen while activity is destroyed

android – Shared element activity transition. View stays on the screen while activity is destroyed

Posted by: admin June 16, 2020 Leave a comment

Questions:

I have two activities. The first one is a splash screen, it has a logo in the center. The second activity has the logo at the top (logo is smaller than on the main). After some time the first activity is closed, and the logo is animated using shared element activity transition. In the first activity’s onStop I finish it (because it’s a splash, and I don’t need it anymore).
The problem is that when I press back in the second activity the logo stands on the screen for some time (2-3 seconds), even when activity is already hidden.
Looks like this: emulator screenshot

First Activity:

public class MainActivity extends BaseActivity {

    @BindView(R.id.logo)
    public ImageView imageView;

    private boolean mShouldFinish;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startSecondActivity();
            }
        }, 3000);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(mShouldFinish) {
            finish();
        }
    }

    private void startSecondActivity() {
        Intent intent = new Intent(this, SecondActivity.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, imageView, "logo");
            startActivity(intent, options.toBundle());
        } else {
            startActivity(intent);
        }
        mShouldFinish = true;
    }
}

SecondActivity:

public class SecondActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }

}

FirstActivity layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.github.guliash.androidexplorer.MainActivity">
    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/ic_android_black_48dp"
        android:layout_gravity="center"
        android:transitionName="logo"
        android:id="@+id/logo"
        android:scaleType="fitCenter"/>
</FrameLayout>

SecondActivity layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_android_black_48dp"
        android:transitionName="logo"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>

What am I doing wrong?

Full code can be found here github repo

How to&Answers:

First, don’t use this mShouldFinish trick, just call finish();
Second, in your second activity try to override onBackPressed and call finish() inside.

Answer:

I edited your code and i tested it was Ok :

SplashActivity :

public class Splash extends AppCompatActivity {



         final int SPLASH_DISPLAY_LENGTH = 1000;
            public ImageView imageView;


        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.activity_splash);
            imageView = (ImageView)findViewById(R.id.logo);


            new Handler().postDelayed(new Runnable(){
                @Override
                public void run() {

                    startSecondActivity();
                }
            }, SPLASH_DISPLAY_LENGTH);
        }

    private void startSecondActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, imageView, "logo");
            startActivity(intent, options.toBundle());
            Splash.this.finish();
        } else {
            startActivity(intent);
            Splash.this.finish();
        }

    }

}

MainActivity :

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

And your Layouts will be :

activity_splash.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="ellip.ir.myapplication.Splash">

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/logo"
        android:layout_gravity="center"
        android:transitionName="logo"
        android:id="@+id/logo"
        android:scaleType="fitCenter"/>

</RelativeLayout>

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="ellip.ir.myapplication.MainActivity">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/logo"
        android:transitionName="logo"
        android:layout_gravity="center_horizontal"/>
</RelativeLayout>

Manifest :

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">

    </activity>
    <activity android:name=".Splash">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>