每当我在我的应用程序中开始一个新活动(即使是一个普通的,空的)时,我看到一个相对较大的延迟(1到2秒)。检查LogCat我看到了:
// This is a transition from CurrentActivity to NewActivity
03-01 14:59:00.195: I/ActivityManager(515): START {act=android.intent.action.GET_CONTENT ...} from pid 9382
03-01 14:59:00.242: D/NewActivity(9382): Intent started
03-01 14:59:00.242: D/CurrentActivity(9382): Activity.onPause
03-01 14:59:00.359: D/dalvikvm(515): GC_CONCURRENT freed 824K, 20% free 10419K/12871K, paused 2ms+12ms
03-01 14:59:00.476: I/cat(416): <4>[22335.131317] CPU1: Booted secondary processor
03-01 14:59:00.476: I/cat(416): <6>[22335.138885] Switched to NOHz mode on CPU #1
03-01 14:59:00.742: W/ActivityManager(515): Activity pause timeout for ActivityRecord{4131c158 CURRENT-ACTIVITY}
03-01 14:59:01.179: D/dalvikvm(9382): GC_CONCURRENT freed 1562K, 68% free 10961K/33415K, paused 2ms+6ms
03-01 14:59:01.937: D/NewActivity(9382): NewActivity.onCreate
请注意,一切正常,没有崩溃,只是这种奇怪的延迟。
如果我将活动中的代码放在DialogFragment
中并显示对话框,那么它几乎是即时的。只有当代码在单独的Activity中运行时才会出现这种延迟。
在CurrentActivity.onPause
内运行的代码是最小的,并且不适用于延迟。
补充:以上是在android API14
下运行时。不同设备上的Android 2.2(API)显示相同的延迟,但LogCat
:
I/ActivityManager(2473): Starting activity: Intent { act=android.intent.action.GET_CONTENT cmp=NewActivity }
03-01 15:08:06.537: D/Editor(4591): Activity.onSaveInstanceState
03-01 15:08:06.537: D/Editor(4591): Activity.onPause
03-01 15:08:06.595: V/InputDevice(2473): ID[0]=0(0) Up(1=>0)
03-01 15:08:07.033: W/ActivityManager(2473): Activity pause timeout for HistoryRecord{CurrentActivity}
03-01 15:08:07.873: D/dalvikvm(4591): GC_FOR_MALLOC freed 5867 objects / 421464 bytes in 33ms
03-01 15:08:08.720: D/NewActivity(4591): NewActivity.onCreate
这是我创建和启动NewActivity的方法: Intent intent = new Intent(this,ImagePickerActivity.class); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intent,ACTIVITY_RESULT_ADD_IMAGE);
CurrentActivity太大而无法发布。
知道造成这种情况的原因以及如何预防吗?
提前致谢,