Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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

like image 883
Guliash Avatar asked May 22 '16 10:05

Guliash


1 Answers

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

like image 114
Shalev Moyal Avatar answered Nov 03 '22 19:11

Shalev Moyal