1

How do I check why an application is running slow? More precisely, which lifecycle method is taking more time to execute in Android.

I have already tried logging the lifecycle methods of each activity and fragment, but I could not figure out the reason for the delay. The onCreate is called, but then there is quite a delay (around 1s) before onResume is called.

Owing to the above delay, the user feels like the application is not very responsive.

The delay is reduced to about 100ms for high end phones. But it the old 2012-2011 models that have this huge delay.

A few ideas on how to investigate further into identifying the root cause of delays, and how could we optimise apps to navigate through screens faster.

Thanks in advance.

B.B.
  • 924
  • 2
  • 11
  • 28
  • onCreateView(), onActivityCreated(), and onStart() is also called between those lifecycle methods. Did you try timing those as well? Also have you tried using the debugger and stepping through to see if there is a particular method call that takes a long time? – arjabbar Jun 20 '16 at 14:14
  • Are the `onCreate` and `onResume` doing a lot of code that could be done asynchronously? Are the layouts to inflate extremely nested and complicated? Did you find any explosive increase in the memory graph which you can't (or can) explain? Are your fragments doing a lot of tasks at the same time they are inflated? Are you connecting to a database (either online, SQLite, Realm or similar) right then? – Sergi Juanola Jun 20 '16 at 14:18
  • Yup, I did all lifecycle methods using a baseActivity and a baseFragment. In most cases, onViewCreated is the slowest. I have already tried reducing the complexity of layouts, by avoiding nested layouts. No database connections, just layout inflation with animation. – B.B. Jun 20 '16 at 14:19
  • Looking into the memory graph minutely would be a good idea. Let me check – B.B. Jun 20 '16 at 14:21

1 Answers1

1

If you are processing heavy load of data (including complex UI rendering) in main thread , then you can find this kind of message in logcat:

W/Trace(1274): Unexpected value from nativeGetEnabledTags: 0
 I/Choreographer(1274): Skipped 55 frames!  The application may be doing too much work on its main thread. 

This may cause your application to slow down with respect to rendering UI

Suggestable Fix

Fixing this requires identifying nodes where there is or possibly can happen long duration of processing. The best way is to do all the processing no matter how small or big in a thread separate from main UI thread. So be it accessing data form SQLite Database or doing some hardcore maths or simply sorting an array – Do it in a different thread

Now there is a catch here, You will create a new Thread for doing these operations and when you run your application, it will crash saying “Only the original thread that created a view hierarchy can touch its views“. You need to know this fact that UI in android can be changed by the main thread or the UI thread only. Any other thread which attempts to do so, fails and crashes with this error. What you need to do is create a new Runnable inside runOnUiThread and inside this runnable you should do all the operations involving the UI. Find an example here.

So we have Thread and Runnable for processing data out of main Thread, what else? There is AsyncTask in android which enables doing long time processes on the UI thread. This is the most useful when you applications are data driven or web api driven or use complex UI’s like those build using Canvas. The power of AsyncTask is that is allows doing things in background and once you are done doing the processing, you can simply do the required actions on UI without causing any lagging effect. This is possible because the AsyncTask derives itself from Activity’s UI thread – all the operations you do on UI via AsyncTask are done is a different thread from the main UI thread, No hindrance to user interaction.

So this is what you need to know for making smooth android applications and as far I know every beginner gets this message on his console.

Community
  • 1
  • 1
Sreehari
  • 5,621
  • 2
  • 25
  • 59
  • Using Asynctask does help, but not for my issue. It's purely related to the UI and the OS perhaps. – B.B. Jun 21 '16 at 13:17