How to avoid black screen on startActivity when FLAG_ACTIVITY_CLEAR_TASK is set?

六眼飞鱼酱① 提交于 2019-11-27 16:22:31

问题


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!


回答1:


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>


来源:https://stackoverflow.com/questions/29015885/how-to-avoid-black-screen-on-startactivity-when-flag-activity-clear-task-is-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!