How do I prevent the status bar and navigation bar from animating during an activity scene animation transition?
There are two approaches you can use that I know of to prevent the navigation/status bar from animating during the transition:
Approach #1: Exclude the status bar and navigation bar from the window's default exit/enter fade transition
The reason why the navigation/status bar are fading in and out during the transition is because by default all non-shared views (including the navigation/status bar backgrounds) will fade out/in in your calling/called Activitys respectively once the transition begins. You can, however, easily get around this by excluding the navigation/status bar backgrounds from the window's default exit/enter Fade
transition. Simply add the following code to your Activitys' onCreate()
methods:
Transition fade = new Fade();
fade.excludeTarget(android.R.id.statusBarBackground, true);
fade.excludeTarget(android.R.id.navigationBarBackground, true);
getWindow().setExitTransition(fade);
getWindow().setEnterTransition(fade);
This transition could also be declared in the activity's theme using XML (i.e. in your own res/transition/window_fade.xml
file):
<?xml version="1.0" encoding="utf-8"?>
<fade xmlns:android="http://schemas.android.com/apk/res/android">
<targets>
<target android:excludeId="@android:id/statusBarBackground"/>
<target android:excludeId="@android:id/navigationBarBackground"/>
</targets>
</fade>
Approach #2: Add the status bar and navigation bar as shared elements
This approach builds off of klmprt's answer, which almost worked for me... although I still needed to make a couple of modifications.
In my calling Activity, I used the following code to start the Activity:
View statusBar = findViewById(android.R.id.statusBarBackground);
View navigationBar = findViewById(android.R.id.navigationBarBackground);
List<Pair<View, String>> pairs = new ArrayList<>();
if (statusBar != null) {
pairs.add(Pair.create(statusBar, Window.STATUS_BAR_BACKGROUND_TRANSITION_NAME));
}
if (navigationBar != null) {
pairs.add(Pair.create(navigationBar, Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME));
}
pairs.add(Pair.create(mSharedElement, mSharedElement.getTransitionName()));
Bundle options = ActivityOptions.makeSceneTransitionAnimation(activity,
pairs.toArray(new Pair[pairs.size()])).toBundle();
startActivity(new Intent(context, NextActivity.class), options);
So far this is essentially the same thing that klmprt suggested in his answer. However, I also needed to add the following code in my called Activity's onCreate()
method in order to prevent the status bar and navigation bar from "blinking" during the transition:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
// Postpone the transition until the window's decor view has
// finished its layout.
postponeEnterTransition();
final View decor = getWindow().getDecorView();
decor.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
decor.getViewTreeObserver().removeOnPreDrawListener(this);
startPostponedEnterTransition();
return true;
}
});
}
Adding the status bar and navigation bar backgrounds as shared elements will force them to be drawn on top of the window's default exit/enter fade transition, meaning that they will not fade during the transition. More discussion about this approach can be found in this Google+ post.
recreate activity with animation not work
I solved doing a kotlin extension:
fun Activity.recreateSmoothly() {
finish()
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
startActivity(Intent(this.intent))
}
and just calling in the activities.
Related Topics
Extending Application to Share Variables Globally
Local Image Caching Solution for Android: Square Picasso, Universal Image Loader, Glide, Fresco
How to Downgrade My Sdk Version
Implementation of Onscrolllistener to Detect the End of Scrolling in a Listview
Reading Text File from Server on Android
Edittext: Disable Paste/Replace Menu Pop-Up on Text Selection Handler Click Event
Fade in Fade Out Android Animation in Java
How to Play Youtube Video in My Android Application
How to Put Gridview Inside Scrollview
How to Get the List of Mounted External Storage of Android Device
What Is the Real Android Studio Gradle Version
Recyclerview - How to Smooth Scroll to Top of Item on a Certain Position
Install_Failed_Update_Incompatible When I Try to Install Compiled .Apk on Device
Open Failed: Eacces (Permission Denied)
Asynctask and Looper.Prepare() Error
The Signing Fingerprint You Specified Is Already Used by Another Android Oauth2 Client
Was Preferencefragment Intentionally Excluded from the Compatibility Package
Vectordrawable - Is It Available Somehow for Pre-Lollipop Versions of Android