在奇怪的延迟后显示活动

时间:2015-08-17 13:18:00

标签: android android-activity delay

我在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");

}

那么我做错了什么?如果延迟是因为进度很大,那么为什么测得的时间似乎很少。假设进度导致延迟,为什么应用程序在显示活动后没有等待?以及如何避免这种情况?

2 个答案:

答案 0 :(得分:0)

与聚合物+ xwalk相比,有一个测试本机应用程序性能的项目。您可以在此处找到来源https://github.com/collabora/xw-perf 这个项目证明本机应用程序运行得更好,但启动活动和GC的延迟是显而易见的。另外有趣的是看GC如何导致fps下降。

答案 1 :(得分:0)

你的问题是Controller.SetMyModel()或setParentActivity()。

为什么我将其缩小到那些?

  1. 你除了那两个电话外,还有时间。
  2. 所有时间似乎都很快。
  3. 这些未定时呼叫都没有源代码。
  4. 这些来电似乎不是具有已知行为的Android方法。
  5. 鉴于您对Controller.SetMyModel()的描述为

      

    实际上只不过是一个基本的二传手

    最有可能的候选者是setParentActivity()。你应该发布它的源代码。

相关问题