安卓系统:活动之间的黑屏



当我从一个活动转到另一个活动时,在事务之间会出现几秒钟的黑屏。在调用startActvity()之前,我正确地完成了该活动。

我的活动使用android:theme="@android:style/theme.Translucent"主题。即使在活动交易之间,也会出现黑屏

谁能告诉我如何解决这个吗

提前感谢:)

在调用startActivity()之前不需要完成活动。

请确保您已在名为"活动"的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>

windows背景在Android 6(棉花糖)

另一种选择是管理转换,因此第一个转换的结束和第二个转换的开始之间没有间隔。但是,您没有提到过渡。

如何在使用抽屉布局打开活动时消除延迟?

如果禁用此默认动画,请创建一种样式:

<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,那么试着在活动B中的OnCreate、OnResume中注释代码。像这样,检查发生了什么——黑屏是否即将到来。如果来了,试着改变主题。

如果您有finish()或FLAG_ACTIVITY_CLEAR_TASK-ICS之前的设备上可能会显示一个空白屏幕

为了避免这种黑屏,你必须在意向中添加一行

overridePendingTransition (0, 0);

示例(kotlin):

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

最新更新