Android – show activity after strange delays

I encountered a delay in starting an activity in the Android application project

Just click the menu item or clickable view, onclicklistener will create a new intent and start the specified activity. So far, it is OK. However, the view will be frozen for a period of time (about 1 second) before the user sees the new activity

That time may be caused by the progress in oncreate, but I measure the time through system. Currenttimemillis() and print it in logcat at the end. Therefore, it seems that it only takes 20-30 milliseconds, and the log will be printed long before the user sees the activity

This is my code:

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

}

So what did I do wrong? If the delay is due to the large progress, why does the measured time seem to be small? Assuming that the progress causes the delay, why does the application not wait after displaying the activity? And how to avoid this?

resolvent:

Compared with polymer xwalk, there is a project to test the performance of native applications. You can find the source here https://github.com/collabora/xw-perf This project proves that native applications run better, but the startup activity and GC latency are obvious. Another interesting thing is to see how GC leads to FPS decline

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>