0

In my app, I am using a storage access framework for writing files on the sd card. For this, I am opening an intent of document tree and inside on activity result I am receiving document URI of the sd card path.

After returning back on the activity result I am getting this document URI and then I am performing a huge task which ends in a black screen as there is a huge load on UI thread. This black screen appears for 7 to 8 seconds.

And I did resolve the black screen issue by using an async task but still other issues arise due to the async task. Instead of a black screen activity screen appears which is OK but on returning to the activity screen the UI progress loader has a problem of skipping frames, On pre execute of an async task the UI loader is skipping frames for 2 seconds and then work fine. I want to reduce skipping frames and also inside async tasks do in background method I have shifted this huge task which is exceeding time limitation and ends in about 1 minute. The huge task with a black screen nearly took 8 seconds as on the main thread without an async task.

Code for black screen

      Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);                         

intent.putExtra("android.content.extra.SHOW_ADVANCED", true);                              

    intent.putExtra("android.content.extra.SHOW_FILESIZE", true);

       startActivityForResult(intent, 77);

and in on activity result performing a huge task

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) {
            sdCardUri = data.getData();
            if(requestCode == 77) {


                documentFile = DocumentFile.fromTreeUri(this, sdCardUri);

 //.... HERE ALOT OF TASK IS BEING PERFORMED AND AS IT IS RETURNING BACK TO ACTIVITY THE MAIN THREAD ENDS IN BLACK SCREEN .....
  HugeTaskPerformed(documentFile);

            }

after using Async task

 if (resultCode == Activity.RESULT_OK) {
            sdCardUri = data.getData();
            if(requestCode == 77) {

              documentFile = DocumentFile.fromTreeUri(this, sdCardUri);              
         // instead of huge task performing async task here
      new MainActivity.BackgroundHugeTask(context).execute();


            }


}

How should I make async do in the background work faster and also in pre execute the loader skipping for 2 seconds and then working accordingly is a mess what changes can I do to minimize them as much as possible?

Cant give you whole code but giving you a sample of where I am stuck.

Muneeb Ali
  • 472
  • 6
  • 14

1 Answers1

0

Without looking into full code it's difficult to say but this link https://stackoverflow.com/a/12850190/11560232 might answer why AsyncTask is very slow.Running on main thread will make the loading slow if data is huge, you might want to try (runOnUiThread()).

Padmini S
  • 143
  • 1
  • 10
  • main thread UI is showing black screen due to huge computation, the computation is to search all directories and finding files according to my app and further compute is done now after moving this computation which is now in async background method takes 10 times more the time , is there any way to make computation work fast in async background. – jaden long Aug 02 '19 at 12:28