簡體   English   中英

Android:活動之間的黑屏

[英]Android: Black Screen between Activity

當我將一項活動轉到另一項活動時,在交易之間會出現幾秒鍾的黑屏。 我在調用startActvity()之前正確完成了活動。

我正在為我的活動使用android:theme="@android:style/Theme.Translucent"主題。 即使在活動交易之間出現黑屏

任何人都可以告訴我如何解決這個問題

提前致謝 :)

在調用 startActivity() 之前不需要完成活動。

確保在被調用的 Activity 的 onCreate 中設置了內容視圖,並且沒有阻塞 UI 線程(如果覆蓋了 onCreate、onStart 和 onResume,請檢查它們)。

您不需要管理完成您的活動,當活動不再可見時,這將自動管理。 只需使用:

startActivity(new Intent(this, MyNextActivity.class));

並在您用來導航活動更改的任何方法中使用此代碼。

如果您確定您的窗口是您活動的背景,您可以將窗口背景設置為黑色以外的顏色:

<item name="android:windowBackground">@drawable/window_background</item>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/window_background"/>
</shape>

Android 6 中的 windowBackground (Marshmallow)

另一種選擇是管理過渡,因此第一個過渡的結束和第二個過渡的開始之間沒有間隙。 但是,您沒有提到轉換。

使用 DrawerLayout 打開 Activity 時如何消除延遲?

要禁用此默認動畫,請創建一種樣式:

<style name="noAnimTheme" parent="android:Theme">
<item name="android:windowAnimationStyle">@null</item>
</style>

並將其設置為清單中活動的主題:

<activity android:name=".ui.ArticlesActivity" android:theme="@style/noAnimTheme">
</activity>

假設 :-

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.xyz);

    // comment code here
    }

如果您從活動 A 轉到 B,則嘗試在 OnCreate 中注釋代碼,在 Activity B 中 OnResume 像這樣並檢查是否會發生黑屏。如果來了,則嘗試更改主題。

如果你有一個 finish() 或 FLAG_ACTIVITY_CLEAR_TASK - 在 ICS 之前的設備上可能會出現一個空白屏幕

為了避免這個黑屏,你必須在意圖中添加一行

overridePendingTransition (0, 0);

示例(科特林):

val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
overridePendingTransition (0, 0)

示例(Java):

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition (0, 0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM