如何避免在设置Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK时出现黑屏?

11

在我的安卓应用中,我需要使用 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK 来设置我的 intent 标志。我可以通过这种方式删除所有之前的 Activities 并启动一个新的 Activity。以下是我的代码:

Intent intent = new Intent(Gerenxinxi.this, MainPart.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
overridePendingTransition(0,0);

然而,当我使用以上代码时,我发现屏幕会闪烁出现黑屏。如果我不设置意图标志为Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK,则黑屏问题将被解决。我的问题是:当设置了Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK时,如何避免黑屏问题?

在写下这个问题之前,我已经发现有人提过类似的问题。这是链接。但是,该问题的答案不能解决我的问题。所以我再次提问,希望有人能够帮助我。谢谢。


你尝试过在finish()之前加入overridePendingTransition(0,0)吗? - Vannen
我之前尝试过,黑屏问题仍然存在。 - LinaInverce
4个回答

25
这个您看到的初始屏幕被称为“预览”屏幕。您可以在主题中声明来完全禁用它:

这个您看到的初始屏幕被称为“预览”屏幕。您可以在主题中声明来完全禁用它:

android:windowDisablePreview

<style name="Theme.MyTheme" parent="android:style/Theme.Holo">
    <!-- This disables the black preview screen -->
    <item name="android:windowDisablePreview">true</item>
</style>

谢谢您的回答,我会立即尝试您的方法~ - LinaInverce
你的解决方案非常有效。感谢你的帮助! - LinaInverce
1
完美的解决方案,谢谢伙计。 - undefined

0

我只找到了一种解决方案,即显示应用程序主题背景而不是黑屏。在我的情况下,它是白色的。

     <application

            android:name="com.my.MyApplication"

            android:allowBackup="true"

            android:icon="@drawable/launcher"

            android:label="@string/app_name"

            android:theme="@style/Theme.MyTheme”>


   <style name="Theme.MyTheme" parent="android:style/Theme.Holo">
        <!-- This disables the black preview screen -->
        <item name="android:windowDisablePreview">true</item>
    </style>

谢谢您的回答,我会立即尝试您的方法~ - LinaInverce

0

试试这个

Intent intent = new Intent(Gerenxinxi.this, MainPart.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

1
谢谢你的回答,我已经尝试了你提供的解决方案,但黑屏问题仍然存在。Darshan Mistry的回答可能会解决这个问题。 - LinaInverce
你是在测试慢速设备还是最近的设备? - Vannen
一部安卓手机,其系统为Android OS 4.2,且它不是一款慢速设备。 - LinaInverce
这段代码可能会完成Darshan的工作。我没错,黑屏是由于Zygote/Android应用程序进程引起的。 - Vannen

0
将此行添加到您的样式中。
android:windowDisablePreview = true.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接