开始新的Android活动太慢了 [英] start new Android Activity is so slow

查看:70
本文介绍了开始新的Android活动太慢了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打开一个新的活动:

I want to open a new Activity:

Intent intent = new Intent(homeScreen.this, EmployeeService.class);         
Bundle b = new Bundle();
b.putInt(Constants.SERVICE_DETAIL_L1_ID_MSG, ServiceIndex.SRV_L1_EMPLOYMENT);
b.putInt(Constants.SERVICE_DETAIL_FOCUS_POS_MSG, 2);
intent.putExtras(b);
startActivity(intent);

但是要花费很长时间才能使目标活动(EmployeeService)可见.从Logcat,我看到:

But it takes so long to make destination Activity (EmployeeService) become visible. From Logcat, I see:

05-14 23:43:31.727: INFO/ActivityManager(59): Displayed activity fr.playsoft.happylille/.employee.EmployeeService: 7050 ms (total 7050 ms)

我不敢相信打开一个新的活动就需要7秒钟以上的时间.我在onCreate()中添加了一个日志,但看到它仅需5毫秒即可完成onCreate.

I cannot believe it take more than 7 seconds just to open a new Activity. I add a log in onCreate() but see it only take 5ms to finish onCreate.

有人可以告诉我如何找到这个问题的根源吗?

Can anyone tell me how to find the root of this problem?

推荐答案

您应该将代码Html.fromHtml(desc);移到线程中以使其异步.在新打开的ActivityonCreate()中可以安全地启动该线程.

You should move the code Html.fromHtml(desc); to a thread to have it asynchronous. This thread can safely be started during the onCreate() of the newly opened Activity.

在该线程的结尾,您可以从UI线程运行tvDesc.setText():

At the end of that thread, you can run tvDesc.setText() from the UI thread:

class ExampleThread extends Thread {
    @Override
    public void run() {
         final Spanned spannedText = Html.fromHtml(desc);

         yourNewlyOpenedActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvDesc.setText(spannedText);
                }
         });
    }
}

更一般而言,一台设备上的7秒可能意味着另一台设备上的20秒,因此当心ANR

More generally, 7 seconds on a device maybe means 20 on another, so beware the ANR!

(下面进一步评论了男孩的评论,此答案的先前版本不再准确/有效)

(Edited further to Boy's comment below, the former version of this answer was no longer accurate/valid)

这篇关于开始新的Android活动太慢了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆