如何避免startActivity黑屏时FLAG_ACTIVITY_CLEAR_TASK设置?黑屏、startActivity、FLAG_ACTIVITY_CLEAR_TASK

2023-09-12 05:14:57 作者:你的暧昧伤透我的心*

我用下面的推出新的活动:

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);

MyNewActivity 推出,显示黑屏。

如果我删除 Intent.FLAG_ACTIVITY_CLEAR_TASK ,该活动没有表现出在任何时候(相反,previous活性示而新的黑屏启动负载)。

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).

编辑:我附上一个很简单的code抄录问题(设定一个黑暗的主题,如 Theme.AppCompat 的应用程序)。黑色的屏幕显示非常短的时间(取决于发射时接收的活动有多少工作呢),但你可以看到它。如果不使用 FLAG_ACTIVITY_CLEAR_TASK ,黑屏不显示和过渡是平滑的:

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>

谢谢!

推荐答案

要完全避免黑屏(又名preVIEW),你可以添加以下到你的 AppTheme

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>

但更好的方法是在 Activity.onCreate 做主线更少的东西,并让显示preVIEW屏幕,同时用户在等待。 为了平稳过渡,您可以设置自定义背景,以您的Windows添加到您的主题:

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>