0

I have an activity DisplayResults that gets started from my MainActivity. In the DisplayResultsActivity, it creates an object of a custom class ProductFinder in its onCreate() function. In ProductFinder's constructor, there is a while loop that takes about 10 seconds to execute.

My problem is that when I start DisplayResult from MainActivity, the whole screen goes black for the duration of the while loop, then finally the DisplayResults activity shows on the screen with everything.

What I would like is to draw the DisplayResults activity normally, then wait for the loop in the ProductFinder class to finish what it's doing without having the entire UI freeze. Having a loading circle in the middle of DisplayResults while the loop is running would be nice.

What is the proper method of doing this? I know that something needs to go in a thread, but I'm not sure which class to put the loading circle in and which code to put inside the thread.

LE GALL Benoît
  • 7,159
  • 1
  • 36
  • 48
Connor S
  • 353
  • 2
  • 12

1 Answers1

0

It is all depends on the requirements

As stated, you can use AsyncTask to do the job but you might get issues such as memory leaks and handling configuration changes(e.g. device rotation).

Second option would be to use AsyncTaskLoader which will let you handle configuration changes (Difference between AsyncTask to AsyncTaskLoader by Google).

Third option, and newer approach, would be to subscribe to ViewModel with MutableLiveData attached to the activity with a background thread to pass the states values (you can also use AsyncTask to manage the states)

Last option (maybe the first) would be to use RxAndroid

dogood
  • 331
  • 2
  • 5