
我在Android应用程序项目中启动一个活动时遇到了延迟.
只要单击菜单项或可单击视图,onClickListener就会创建一个新的Intent并启动指定的活动.到目前为止还可以.但是,在用户看到新活动之前,视图会被冻结一段时间(大约1秒).
那个时间可能是由onCreate里面的进展引起的,但是我通过System.currentTimeMillis()测量时间并在结尾处将其打印在logcat中.因此,它似乎只需要20-30毫秒,并且在用户看到活动之前很久就会打印日志.
这是我的代码:
public class ExampleActivity extends MyActivity { MyModel myModel; long startTime; protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); startTime = System.currentTimeMillis(); setContentVIEw(R.layout.activity_ders_programi); setToolbar(); myModel = Controller.getMyModel(); if(myModel == null) { //If we don't have the object prevIoUsly //this fills the object, takes some time myModel = new MyModel(this); //myModel is observable and this activity is observer. //Whenever it's done, it notifIEs this activity } else //If we got the object prevIoUsly createCardVIEws(); Controller.setMyModel(myModel); setParentActivity(); } //If Observable object notifIEs this activity, update() is called. @OverrIDe public voID update(Observable observable, Object o) { createCardVIEws(); } //Creating a List of cardVIEws, this also takes some time private voID createCardVIEws() { ArrayList<Card> cards = new ArrayList<Card>(); for(int i = 0; i < 5; i++) { MyModelCardModel card = new MyModelCardModel(this, i); card.init(); cards.add(card); } CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(this,cards); CardListVIEw ListVIEw = (CardListVIEw) findVIEwByID(R.ID.my_card_List_vIEw); if (ListVIEw!=null) ListVIEw.setAdapter(mCardArrayAdapter); //I think here is the last point. //After this point there will be no significant process. long stopTime = System.currentTimeMillis(); Log.d("modelflow", "card create takes: " + (stopTime - startTime) + "ms");}那么我做错了什么?如果延迟是因为进度很大,那么为什么测得的时间似乎很少.假设进度导致延迟,为什么应用程序在显示活动后没有等待?以及如何避免这种情况?
解决方法:
与聚合物xwalk相比,有一个测试本机应用程序性能的项目.你可以在这里找到来源https://github.com/collabora/xw-perf
这个项目证明本机应用程序运行得更好,但启动活动和GC的延迟是显而易见的.另外有趣的是看GC如何导致fps下降.
以上是内存溢出为你收集整理的android – 在奇怪的延迟后显示活动全部内容,希望文章能够帮你解决android – 在奇怪的延迟后显示活动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)