How to avoid black screen when Intent.FLAG_ACTIVIT

2020-06-03 04:44发布

问题:

In my android app, I need to use Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK to set my intent flag. I can remove all my previous Activities and start a new Activity in this way.This is my code below:

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

However,I found a flicker of black screen when i use the code above. If I don't set intent flag with Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK , the flicker of black screen will be gone.My question is: what can I do to avoid the black screen when Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK is set ?

Before I write down this question, I have found that someone has the similar question.This is the link However,the answers of this question can not solve my problem.So I ask the question again.I hope anyone can help me.Thank you.

回答1:

This initial screen that you see is called the "Preview" screen. You can disable this completely by declaring this in your theme:

android:windowDisablePreview

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


回答2:

Try this

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


回答3:

only one solution i found it instead of showing black screen show app theme background. in my case its white

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


回答4:

Add this line in your style

android:windowDisablePreview = true.