English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Método de solución del problema de la pantalla blanca y negra en la interfaz de inicio de Splash de Android

前言

我相信很多Android开发同学都遇到过这样的需求:

 1.实现一个Splash界面,界面上有应用相关的背景图片和一个开始按钮.
 2.点击按钮之后进入主页,以后用户再打开应用就不显示这个Splash界面了. 

也相信很多同学都遇到了这样的困惑:

•第二次进入应用,尽管你在Splash界面已经直接跳转到首页了,但是还是有个白屏或者黑屏或者带ActionBar的白屏闪现一下.

如果你也遇到这个问题,那就继续阅读这篇文章,我带大家去分析和解决这个问题.

解决方案

这里我们先给出解决方案,然后再具体分析产生原因哈.避免分析的大段文字阻碍了同学学习的热情.

解决方案非常简单,一句话概括是:给Splash Activity设置一个主题,主题内容是:全屏+透明.

style.xml增加SplashTheme主题:

<style name="SplashTheme" parent="AppTheme">
 <item name="android:windowFullscreen">true</item>
 <item name="android:windowIsTranslucent">true</item>
</style>

AndroidManifest.xml中为SplashActivity配置主题:

<activity android:name=".activity.SplashActivity">
 android:theme="@style/SplashTheme">
 <intent-filter>
 <action android:name="android.intent.action.MAIN"> />
 <category android:name="android.intent.category.LAUNCHER"> />
 </intent-filter>
</activity>

After such configuration, the white screen, black screen, and ActionBar screen that困扰 (bother) you should have disappeared. To understand the why as well as the how, I hope that students can continue to analyze with me the reasons for the generation of these white screens.

The process of window startup of the Activity component

First of all, it is declared that this section of content refers to Professor Luo Shengyang's blog in large quantities. For the convenience of understanding, the content has been compressed. If there is any infringement, I will delete this analysis immediately.

To understand the root cause of the white screen, it is necessary to trace the process of window startup of the Activity component. During the startup process, the Activity component calls the startActivityLocked method of the ActivityStack class. Note that when calling the startActivityLocked method of the ActivityStack class, the Activity component is still in the startup process, that is, its window has not yet been displayed, but at this time, the ActivityManagerService service will check whether it is necessary to display a startup window for the Activity component that is being started. If necessary, the ActivityManagerService service will request the WindowManagerService service to set a startup window for the Activity component that is being started (ps: this startup window is the origin of the white screen).

1. ActivityStack.startActivityLocked

public class ActivityStack {
 // set to false to disable the preview that is shown while a new activity
 // is being started.
 static final boolean SHOW_APP_STARTING_PREVIEW = true;
 private final void startActivityLocked(ActivityRecord r, boolean newTask, boolean doResume) {
 final int NH = mHistory.size();
 int addPos = -1;
 // Place to new activity at top of stack, so it is next to interact
 // with the user.
 if (addPos < 0) {
  addPos = NH;
 }
 // Coloque la actividad en la pila de historial y proceda
 mHistory.add(addPos, r);
 if (NH > 0) {
  // Queremos mostrar la ventana de vista previa de inicio si estamos
  // pasando a una nueva tarea o el proceso de la siguiente actividad es
  // no está ejecutándose actualmente.
  boolean showStartingIcon = newTasks;
  ProcessRecord proc = r.app;
  if (proc == null) {
  proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
  }
  if (proc == null || proc.thread == null) {
  showStartingIcon = true;
  }
 }
 }
}

Aún no ha terminado... esperamos que sigan prestando atención.

Esto es todo el contenido del artículo, espero que ayude a su aprendizaje y que todos lo apoyen a la enseñanza de gritos.

Declaración: el contenido de este artículo se obtiene de la red, pertenece al propietario original, el contenido se contribuye y carga de manera autónoma por los usuarios de Internet, este sitio no posee los derechos de propiedad, no se ha realizado un procesamiento editorial humano y no asume la responsabilidad de las responsabilidades legales relacionadas. Si encuentra contenido sospechoso de copyright, por favor envíe un correo electrónico a: notice#oldtoolbag.com (al enviar un correo electrónico, por favor reemplace # con @) para denunciar, y proporcionar evidencia relevante. Una vez verificada, este sitio eliminará inmediatamente el contenido sospechoso de infracción.

Te puede gustar