I am launching a new activity using the following:
Intent intent = new Intent(this, MyNewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(0, 0);
While MyNewActivity
is launching, a black screen is shown.
If I remove Intent.FLAG_ACTIVITY_CLEAR_TASK
, the activity is launched without showing a black screen at any moment (instead, the previous activity is shown while the new one loads).
Is there any way to avoid this black screen? Removing the flags seems to not be an option (I need to clear all the current task's stack and launch a new activity as the root one).
EDIT: I attach a very simple code which reproduces the issue (set a dark theme such as Theme.AppCompat
for the app). The black screen is shown for very little time (depends on how many work the receiving activity does when launching), but you can see it. If you don't use FLAG_ACTIVITY_CLEAR_TASK
, the black screen is not shown and the transition is smooth:
MainActivity
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyNewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(0,0);
}
});
}
}
MyNewActivity
public class MyNewActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLICK ME!" />
</RelativeLayout>
activity_new.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am the new activity!" />
</RelativeLayout>
Thanks!
To completely avoid the "black screen" (aka "preview") you can add the following to your AppTheme
:
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:windowDisablePreview">true</item>
...
</style>
However a better approach is do less things on main thread during Activity.onCreate
and let show the preview screen while user is waiting.
For a smooth transition, you can set a custom background to your windows adding this to your theme:
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:windowBackground">@drawable/slash_screen</item>
...
</style>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With