2

I am using an Asynctask to do some processing, after which a UI List is updated with the remaining/ additional items (addition or deletion case).

The functionality works properly in the debug apk that I work on in Android Studio. However, it is not working in the release APK file.

When I analysed further, I am seeing that the functionality is showing correct result when I lock and unlock the screen, or if I close and reopen the app. It fails only in producing an instantaneous update.

This is why I felt there is something wrong in the asynctask.

public class RefreshDisplayAsyncTask extends AsyncTask<Void, Void, Void> {
private Activity curActivity = null;
private boolean shouldProceed = false;

public RefreshDisplayAsyncTask(Activity activity)
{
    curActivity = activity;
}
protected Void doInBackground(Void... params) {
    //Log.d("UIBug","RefreshTask entered");
    ((MainActivity)curActivity).setUpList();
    shouldProceed = false;
    curActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            ((MainActivity)curActivity).listView.setAdapter(new listArrayAdapter(curActivity,((MainActivity)curActivity).list1,((MainActivity)curActivity).list2));

            shouldProceed = true;
        }
    });
    while(!shouldProceed){}
    //Log.d("UIBug","RefreshTask completed");
    return null;
}

@Override
protected void onPreExecute() {

    super.onPreExecute();
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(Void result) {

//        if(progressDialog!=null)
//      {
//        progressDialog.dismiss();
  //  }
 }


}

Why is the signed apk producing incorrect behaviour while the unsigned apk is behaving properly? How to rectify this?

SoulRayder
  • 5,072
  • 6
  • 47
  • 93
  • 1
    First of all: NEVER pass your activity to your asyncTask, this creates memory leaks when the async task is made to persist across activity recreations. – A. Steenbergen Feb 26 '17 at 17:37
  • Thanks for the input. But any idea why this is working in unsigned apk and not in signed apk? And any way to rectify this? – SoulRayder Feb 26 '17 at 17:39
  • 1
    It might be because of tougher restrictions on non-debug apps by the system. There is a lot of unsave code in your example, for example the while(!shouldProceed) part is atrocious. You are not using the AsyncTask as indended. You should rewrite it to use onPostExecute instead of the above mentioned while loop for startes. – A. Steenbergen Feb 26 '17 at 17:42
  • Yes.. I wrote this a long time back.. correcting everything now.. but I was intrigued by this seemingly buggy aspect in the signed apk vs correct behaviour in the unsigned apk – SoulRayder Feb 26 '17 at 17:43
  • 1
    It might have just been a fluke, since there are a lot of things that can go wrong with this code example. I would clean up the code and bring it in line with AsyncTask specifications and try again. If it still doesnt work, tell me and I can have a look – A. Steenbergen Feb 26 '17 at 17:46
  • Thanks a lot for your help.. the while loop was the culprit :) Post an answer and I would be happy to accept it :) Learned something new today regarding signed vs unsigned-app behaviour. – SoulRayder Feb 26 '17 at 17:49
  • 1
    I worked on this over a year ago, but I still don't understand why I put that loop there. It didn't impact my app then since it was majorly running on Android 4.4 and 5, but the bug manifested in devices on Android 6.0. – SoulRayder Feb 26 '17 at 17:58
  • That's alright, I didn't really do anything here, so no need for an answer. I am glad it is working for you now :) – A. Steenbergen Mar 02 '17 at 17:52
  • More often than not, it's the little things that count :) It's up to you finally :) But thanks anyways :) – SoulRayder Mar 02 '17 at 18:12
  • I added an answer for future people who encounter the same problem! – A. Steenbergen Mar 05 '17 at 14:03

1 Answers1

2

First of all: NEVER pass your Activity to an AsyncTask, this creates memory leaks when the AsyncTask is made to persist across Activity recreations and is considered bad practice. If you have to pass data around, use something like EventBus instead.

Your code example might fail because of tougher restrictions on non-debug apps by the system.

In any case, there is a lot of unsave code in your example. For instance the while(!shouldProceed) part is atrocious.

Generally you are not using the AsyncTask as indended. You should rewrite it to utilize onPostExecute instead of the above mentioned while loop for starters. Please see here for further info on how an AsyncTask should be structured and follow that structure.

As discussed in the comments, cleaning up the AsyncTask solved your problem.

A. Steenbergen
  • 3,360
  • 4
  • 34
  • 51