简体   繁体   中英

Cannot make animation work when switching between activities

I want to have a fade in and out animation when I switch between activities. In my "LoginActivity", I created this method

case R.id.btnGoToSettings:
    ActivityOptions options =
            ActivityOptions.makeCustomAnimation(this, android.R.anim.fade_in, android.R.anim.fade_out);
    startActivity(new Intent(this, AmsSettingsActivity.class), options.toBundle());
    break; 

This doesn't work.

I also tried this approach

Bundle bundle = ActivityOptionsCompat.makeCustomAnimation(getApplicationContext(),
                android.R.anim.slide_in_left, android.R.anim.slide_out_right).toBundle();


case R.id.btnGoToSettings:
    startActivity(new Intent(this, AmsSettingsActivity.class), bundle);
    break;

And this one too:

startActivity(new Intent(this, AmsSettingsActivity.class));
    finish();
    overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);

This is my manifest file

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

and in my styles:

    <style name="AppBaseTheme" parent="android:Theme.Light"></style>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowActivityTransitions">true</item>
    </style>

    <style name="Theme.SunmiTest" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:windowActivityTransitions">true</item>

        <item name="colorPrimary">@color/background</item>
        <item name="colorPrimaryDark">@color/background</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorControlNormal">@color/white</item>
        <item name="colorControlActivated">@color/white</item>
    </style>

How do I make this work? Thanks.

You can use this code before setContentView() of AmsSettingsActivity this activity

    overridePendingTransition(android.R.anim.slide_in_left,
android.R.anim.slide_out_right);

This will show your animation when switching between activities

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