In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Android optimization to improve the application startup speed and Splash page design method", in the daily operation, I believe that many people have doubts in Android optimization to improve the application startup speed and Splash page design method. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Android optimization to improve the application startup speed and the design method of Splash page". Next, please follow the editor to study!
1. There are two ways to start.
1) Cold start: when you start directly from the desktop and there is no cache for the process in the background, the system needs to recreate a new process and allocate various resources.
2) Hot start: the app background has the cache of the process, and the process started at this time belongs to the hot start.
Hot start does not need to reassign the process, and will not Application, just go to the entrance Activity of app, so the speed is very fast.
two。 How to measure the startup time of an application
Use the command line to start app while taking time measurements. Unit: milliseconds
Adb shell am start-W [PackageName] / [PackageName.MainActivity]
Example: adb shell am start-W com.haocai.app/.activity.GuideActivity
Hot start time:
The printed result is:
ThisTime the activity takes time to start
Startup time of TotalTime application itself = startup time of resources such as ThisTime+ application application
WaitTime system startup application time = TotalTime+ system resource startup time
3. Process initiated by the application
Application starts with the constructor-> attachBaseContext ()-> onCreate ()
Activity construction method-- > onCreate ()-- > set the layout of the display interface, set the theme, background, and other properties-- > onStart ()
-> onResume ()-- > display the View inside (measure, layout, draw, display to the interface)
We know from the construction method that the startup time is mainly spent in each startup process.
4. Reduce the time it takes to start an application
According to the application startup process, we get the following suggestions to reduce the time-consuming operation of application startup:
Do not initialize time-consuming operations in the constructor, attachBaseContext (), onCreate () of Application.
MainActivity, because the user only cares about the last frame displayed, the level of our layout requires less time for custom controls to measure, layout, and draw. At the same time, do not do time-consuming operations in onCreate, onStart, onResume.
For initialization of SharedPreference
Because when it is initialized, it needs to read all the data out and put it in memory.
Optimization 1: reduce the number of sp files as much as possible (IO takes time)
Optimization 2: initialization like this is best placed in a thread
Optimization 3: a large amount of data is cached into the database
The main time consuming for app startup is: Application initialization + MainActivity interface loading and drawing time.
Because the business and layout complexity of MainActvity is so high, even the interface must have some initialized data before it can be displayed.
At this time, MainActivity may not be able to get out for half a day, which makes users feel that the app is too stuck.
General methods:
1. What we need to do is to give users a quick and clean experience. Click app and our interface pops up immediately.
So I thought of using SplashActivity--, a very simple welcome page that shows only one picture without doing anything.
two。 But after SplashActivity starts, you still need to jump to MainActivity. MainActivity still needs to load the layout and data from scratch.
Think of SplashActivity can do some MainActivity data preloading. Then you need to pass it to MainActivity through intention.
Better optimization:
Time-consuming issues: Application+Activity startup and resource loading time; time spent preloaded data.
If we can make the two times overlap and do these two things concurrently in the same period of time, it will save time.
For example: combine SplashActivity and MainActivity into one.
Once in, it still shows that MainActivity,SplashActivity can be turned into a SplashFragment, and then put a FrameLayout as the root layout to display the SplashFragment interface directly.
SplashFragment inside is very simple, is the reality of a picture, start very fast.
When the SplashFragment is displayed, then remove it. At the same time, the network data cache is carried out in the 2S friendly time of splash.
This is when we see the MainActivity, so we don't have to wait for the network data to return.
New problem: SplashView and ContentView loading are done together, which may affect the startup time of the application?
Solution: you can use ViewStub to delay loading View in MainActivity to mitigate this effect.
ViewStub is designed to prevent MainActivity startup from being too time-consuming to load resources. Delayed loading does not affect startup and is user-friendly.
But viewStub loading also takes time. When the main interface comes out.
ViewStub.inflate (xxxx)
5. How to design delayed loading DelayLoad
The first thing that comes to mind is to call the Handler.postDelayed () method in onCreate
Question 1: how to control the delay time
The startup speed of different machines is different, how to control this time?
Suppose you first need splash to do a 2s animation, and then close the splash page after loading the main interface in MainActivity
If it is written like this:
MHandler.postDelayed (new Runnable () {@ Override public void run () {mProgressBar.setVisibility (View.GONE); iv.setVisibility (View.VISIBLE);}}, 2500)
It is impossible to load on the accurate monitoring page.
Question: when the application has been started and loaded, the interface has been displayed.
After the implementation of onResume, the display is finished? no way.
It is recommended that you use getDecorView () to get the superior view and then add the view
For the comprehensive appeal programme, the following is the key code:
Public class MainActivity extends AppCompatActivity {private Handler mHandler = new Handler (); private SplashFragment splashFragment; private ViewStub viewStub; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); splashFragment = new SplashFragment (); FragmentTransaction transaction = getSupportFragmentManager (). BeginTransaction (); transaction.replace (R.id.frame, splashFragment); transaction.commit () / * it is not good to judge whether the view has finished loading * / mHandler.postDelayed (new Runnable () {/ / @ Override// public void run () {/ / mProgressBar.setVisibility (View.GONE); / / iv.setVisibility (View.VISIBLE); / /} /}, 2500) ViewStub = (ViewStub) findViewById (R.id.content_viewstub); / / 1. Judge when the form is loaded Immediately load the real layout into getWindow (). GetDecorView (). Post (new Runnable () {@ Override public void run () {/ / turn on delayed loading mHandler.post (new Runnable () {@ Override public void run () { / / load viewstub into viewStub.inflate () }); / / 2. Judge to execute when the form is loaded and delay the animation for a period of time. GetWindow () .getDecorView () .post (new Runnable () {@ Override public void run () {/ / enable delayed loading, or it can be executed immediately without delay (I delay here to achieve the time-consuming animation in fragment) mHandler.postDelayed (new DelayRunnable (MainActivity.this, splashFragment), 2000);}}) / / 3. At the same time, the data is loaded asynchronously. } static class DelayRunnable implements Runnable {private WeakReference contextWeakReference; private WeakReference splashFragmentWeakReference; public DelayRunnable (Context context, SplashFragment f) {contextWeakReference = new WeakReference (context); splashFragmentWeakReference = new WeakReference (f) } @ Override public void run () {/ / remove Fragment if (contextWeakReference! = null) {SplashFragment splashFragment = splashFragmentWeakReference.get (); if (splashFragment = = null) {return;} FragmentActivity activity = (FragmentActivity) contextWeakReference.get () FragmentTransaction transaction = activity.getSupportFragmentManager (). BeginTransaction (); transaction.remove (splashFragment); transaction.commit ();}
Activity_main.xml:
Ps: the test data are time-consuming data obtained from the old Samsung phone.
However, compared with the SplashActivity+MainActivity startup speed optimization is still quite obvious. You can try it on your mobile phone.
At this point, the study on "Android optimization to improve the startup speed of applications and the design method of Splash pages" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.