James Jones
James Jones

Reputation: 572

Best way to show spinner/loading while retrieving data in Android, Lollipop+

So I'm looking for the best way to show a spinner/loading screen while my data is being fetched.
I know there have been a ton of questions, but the questions/answers have been submitted 2+ years ago.

I'm looking for something more current and more agreeable to Google's best practices.

Upvotes: 0

Views: 993

Answers (2)

Eenvincible
Eenvincible

Reputation: 5626

Consider this approach:

  • Use Events - this decouples your application code and here is how you can achieve what you want:
  • If you use a library like EventBus, you simply create an Event class with a variable called loadingCompleted - you can then set its value when the loading is completed to true.
  • Now, the way EventBus works is that you can register to listen to events in your activity or fragment then override onEvent(YourEventClass event) method.
  • Once your loading is finished, you can notify the activity or fragment by doing a post - again, EventBus documentation shows how to easily do this.
  • Inside onEvent() method in the subscribing class, you simply dismiss the progress dialog as long as loadingCompleted is set to true.
  • Another idea to avoid showing the progress dialog all the times even when there is no new data to load is perhaps set a variable in your activity to check if you need to load the data or not.

You can find the library and how to use it here

I hope this gives you an idea. Good luck.

Upvotes: 1

Bojan Kseneman
Bojan Kseneman

Reputation: 15668

SwipeRefreshLayout is pretty modern an pretty popular too. I use it a lot. Here is the desing pattern and some video animations showing you what it will look like. Here is some basic example how to implement it, it's pretty simple too.

Upvotes: 2

Related Questions