0

I have a performance problem.

So I'm writing a chat application and I have a ViewPager with Fragments in it for each separate chat, the ViewPager lives in ChatActivity, and the messages in the Fragments are displayed via a simple ListView.

Everything is smooth as long as I "minimize" my application. When the app is minimized, and I receive, say just 10 messages during that time, and return back to the activity, there is a noticeable delay (e.g. the standard Android Application restore animation does not play). If I minimize the app again, and restore it instantly, the delay is completely gone (assuming no new messages arrived).

There seems to be no correlation in the performance to the amount of new messages that have arrived: whether there are 10 or 500 new messages, the delay is the same.

I receive messages from a Socket, pass them to the Chat object, which simply passes them to the ChatFragment, which just uses listView.post() to post them into the ListView.

My ChatFragment and ListView's adapter are pretty trivial so I don't post them here, the messages are stored in an ArrayList in the Chat object (the adapter just pulls them from there).. I'm really not doing anything "fancy".

To put it simply, when my Activity (and the Fragments in it) are paused, and new data is posted to the ListView, the ListView (I assume) has to do "more work at once" when the Fragment is resumed and the ListView is "refreshed".

This might be a bit vague without code, but perhaps someone can tell me if my principle is at least right (i.e. simply post() the messages to the adapter, regardless if the Activity and Fragment are visible to the user?).

If anyone has any ideas what might cause this delay, I'd be very grateful.

4

1 回答 1

0

我对新消息和ListView问题的直觉是完全错误的!导致速度变慢的原因是我有一个启动器活动,它启动以前可见的活动或在没有活动的情况下自行显示。我没有调用它,而是简单地调用finish()了!这导致活动继续其生命周期,在幕后做任何事情,导致速度变慢。onCreate()return

于 2013-08-23T18:40:22.170 回答