As more and more functions are being developed for Android applications, the startup speed is getting slower and slower. Is there a way to make your application start faster?
Methods are developed by humans. Let's talk about my implementation methods:
1. Move the content initialized in oncreate to the thread for initialization and loading.
2. After Initialization is complete, send a message through handler,
3. After receiving the message from hander, initialize the complete interface.
With this idea, the interface can be quickly displayed, rather than a black screen.
Code prototype:
Public class mainactivity extends activity {
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. activity_main); // set layout Layout
// Use a thread 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 = 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); // It takes 5 seconds to simulate Data Loading
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
// The data loading is complete. You can update the interface.
Handler. sendemptymessage (msg_init_view );
}
Private void initview (){
// Refresh the todo Interface
}
}