Android application startup speed optimization

  • 2020-12-20 03:45:28
  • OfStack

In developing Android applications, the startup speed becomes slower and slower as more features are added. Is there a way to make your app start up 1 point faster?

The method is man's idea. Let's start with my implementation method:

1. Move the contents initialized in onCreate to the thread for initialization, load, etc

2 After initialization is complete, send a message through Handler,

3 After receiving the message in Hander, the complete interface is initialized again.


public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // Set up the layout population 

    //  Use threads to load data asynchronously without blocking the interface. 
    new Thread(){

      @Override
      public void run() {
        // TODO Auto-generated method stub
        super.run();
        initData();
      }
      
    }.start();
  }

  private final static int MSG_INIT_VIEW = 0xA00;
  private final Handler handler = new Handler() {

    @Override
    public void dispatchMessage(Message msg) {
      switch (msg.what) {
      case MSG_INIT_VIEW:
        initView();
        break;
      default:
        super.dispatchMessage(msg);
      }
      
      
    }
    
  };
  
  private void initData(){
    try {
      Thread.sleep(5000);//  Simulate load data required  5 seconds 
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    // The data is loaded and the interface is ready to be updated 
    handler.sendEmptyMessage(MSG_INIT_VIEW);
  }
  
  private void initView(){
    //TODO  Refresh the interface 
  }

}

That is the end of this article, I hope you learn Android software programming is helpful.


Related articles: