Faster loading times for activities in android app
The way I have my app set up now the initial activity that launches displays an options screen which has 4 buttons. Each button when clicked.launches its own activity and layout that goes with it. But when the user clicks a button it takes almost 3-5 seconds.for the activity to open the new screen. Is there a better way of doing this so that it loads faster? Is there a way to launch all of the activities in the background at the start of the app so they are already running? What woi开发者_Python百科luld be the best/recomended way of dealing with this.
By the sounds of it, your activities are doing too much in their onCreate method. Try moving some of the activities setup (including loading images etc) into AsyncTaks. This should hopefully reduce the load time of your activities.
In addition to Laurence Dawson's answer, it could be a large amount of data passing through the intent that cause that lag. In this case I'd consider to change the code from using separate activities to one activity with ViewFlipper to avoid parceling/unparceling that data.
Also you can move a lot of initial setup to a class that extends the Application class. Moving setup from activities to Application will speed up your app as Application class runs only once when the app is first initiated. More info: http://developer.android.com/reference/android/app/Application.html
- You will achieve significant launch speed increase by using one FragmentActivity with Fragments rather then several activities.
- Do as little as you can in onCreate (onCreateView) and load any data in the background thread.
- Create smart layouts with as few levels of hierarchy as possible. If you load pictures - scale them to actual view size and load asynchronously (use Glide or Picasso).
- If you still have complex layout you can use AsyncLayoutInflater to inflate it asynchronously. https://developer.android.com/reference/android/support/v4/view/AsyncLayoutInflater.html
精彩评论