1

I'm creating an API for an app. When the app launches (MainActivity.onCreate()) an initialization call is made. This initialization causes the UI to lag for a second though, and I want to get rid of the lag. The sequence of (larger) events in the initialization call is as follows:

(1) getDeviceID() generates a UID

(2) setBuild() uses StringBuilder to append strings together and then sha1 it (code available upon request)

(3) create an instance of a subclassed AsyncTask thread called DopamineRequest. This sends an HTTP request

(4) getInitRequest() creates a JSONObject by iterating through a couple of LinkedHashSet<String>s and also using System.currentTimeMillis()

(5) execute the AsyncTask thread which only has the doInBackground() function overridden

If I understand how AsyncTask works, it's executed in the background so should have no effect on the UI unless the UI calls asyncTask.get() which blocks it.

Is some other function causing the lag or is it AsyncTask?

protected static void initBase(Context c){
        if(context!=null)   // already been initialized
            return;

        context = c;
        if(identity == null) {
            identity = new HashMap<String, String>();
        }
        identity.put( "DEVICE_ID", getDeviceID() );           // (1)
        setBuild();                                           // (2)

        URIBuilder uri = new URIBuilder(appID);

        DopamineRequest initRequest = new DopamineRequest();  // (3)
        String initRequest = getInitRequest();                // (4)
        initRequest.execute(uri.getURI(URIBuilder.URI.INIT), initRequest) // (5);

    }
Community
  • 1
  • 1
  • 4
    Use Traceview and figure out where your problem actually lies. – CommonsWare Sep 12 '14 at 22:48
  • are you doing anything in your onPreExecute() because that is invoked in the UI thread. – android_Muncher Sep 12 '14 at 23:15
  • It is almost certainly some other function. Besides @CommonsWare's suggestion (which is a good one) you can remove one line at a time or post it to a Handler that runs after your UI is displayed. – Jim Sep 12 '14 at 23:21
  • @CommonsWare thank you so much for suggesting Traceview! [getDeviceID()](http://stackoverflow.com/a/10294536/2561963) was causing the lag, mainly String.toUpperCase() – cuddergambino Sep 13 '14 at 03:24

1 Answers1

0

I found out the answer using Traceview. AsyncTask can't be slowing down the UI unless onPreExecute() or get() is called