如何避免启动前出现黑屏?
我创建了一个带有背景和进度条的加载页面,当它完成加载时,它启动主类,但加载屏幕没有显示。加载时只是黑屏,然后是主屏幕。我把所有的工作都放在 onResume 中,我也尝试过 onStart 但没有运气,
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loadpage);
//get rid of title bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(1);
}
@Override
public void onResume() {
super.onResume();
words = WordList.sharedWordList(this);
if(generatedLevels==null)
{
generatedLevels = new ArrayList<PuzzleMZLen>();
}
if(!p.isAlive())
{
p.start();
}
Intent i = new Intent(getApplicationContext(), Main.class);
startActivity(i);
}
提前谢谢
I created a loadpage with a background and a progress bar and when it finishes loading, it starts the main class but the loading screen is not showing up. It is just a black screen while it loads and then the main screen. I put all the work in the onResume and I also tried onStart with no luck
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loadpage);
//get rid of title bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(1);
}
@Override
public void onResume() {
super.onResume();
words = WordList.sharedWordList(this);
if(generatedLevels==null)
{
generatedLevels = new ArrayList<PuzzleMZLen>();
}
if(!p.isAlive())
{
p.start();
}
Intent i = new Intent(getApplicationContext(), Main.class);
startActivity(i);
}
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须使用 AsyncTask 来完成此类工作。您的布局已填充加载完成后。所以你看到的是黑屏。
使用
AsyncTask
类的onPreExecute
来显示进度条。并在doInBackground
方法中编写加载代码。You have to use AsyncTask for this kind of work.Your layout is populated after the completion of the loading.So you are watching black screen.
Use
onPreExecute
ofAsyncTask
class to show the progress bar. And write loading code in thedoInBackground
method.一定要使用 AsyncTask 来实现这一点。我的应用程序也有同样的情况,这是我的代码:
onPreExecute
方法加载一个仅显示“正在加载。请稍候...”的 ProgressDialog。doInBackground
执行使我的应用程序加载的操作(在我的例子中,从服务器抓取和解析文本),然后onPostExecute
填充旋转器,然后关闭 ProgressDialog。您可能希望进度条有一些不同的东西,但 AsyncTask 将非常相似。在onCreate
中使用 newgetAllData.execute(this);
调用它Definitely use AsyncTask for this. I had the same thing going for my app, and here's my code:
onPreExecute
method loads a ProgressDialog that just shows "Loading. Please wait...".doInBackground
does what was making my app load (in my case grabbing and parsing text from a server) and thenonPostExecute
is filling a spinner then dismissing the ProgressDialog. You'll want some thing different for a progress BAR, but the AsyncTask will be very similar. Call it inonCreate
with newgetAllData.execute(this);
有一个很好的方法来创建良好的飞溅活动。
为了防止 ANR,您应该将所有数据加载移到 UI 线程之外,正如其他答案中提到的那样。请使用 AsyncTask 或任何其他类型的多线程。
然而,要消除在某些慢速设备上显示片刻的烦人黑屏,您需要执行后续步骤:
bg_splash
可绘制对象。它将及时显示给用户,而不是黑色背景。例如,它可以是品牌颜色背景上的品牌标志。创建闪屏主题并将其放入
/res/values/themes.xml
:不要忘记通过更新
将创建的主题分配给闪屏活动>AndroidManifest.xml
:There is a good way how to create a good splash activity.
To prevent ANR you should move all data loading outside of UI thread as it was mentioned at other answers. Please use AsyncTask or any other kind of multi-threading for that.
However to remove annoying black screen which shows for few moments on some slow devices you need to do next steps:
bg_splash
drawable. It will be shown instead of black background just in time activity shows to user. For example it can be brand logo on brand color background.Create a splash screen theme and put it into
/res/values/themes.xml
:Don't forget assign created theme to splash activity by updating
AndroidManifest.xml
: