-6

I am using volley library to perform network operations. On Application launch, I hit the service, I want to stop all the operations until i get the response from the service.

So i want to perform this synchronously. As I am using Volley which by default works in a separate thread. So how can i do this.

I have created custom Interface/listener to handle this, but does Android provide some way to achieve this.

I have done following.

Splash Activity implements an interface, and it goes to Main Activity after data is loaded

@Override
public void onContainerLoaded() { 
   //startActivity(MainActivity)
 }
dev90
  • 7,187
  • 15
  • 80
  • 153

3 Answers3

1

Even if you want to, you should definitely never EVER run any network-related task synchronously.

What you can do instead is starting your activity normally, and replace your layout with a progressbar logo, that is set to visibility.gone when your task is completed.

EDIT : By the way, if you are just starting your app and you haven't done anything concrete yet, I would recommend you to use an AsyncTask instead of Volley, which is often causing layer-coupling mistakes.

Arthur Attout
  • 2,701
  • 2
  • 26
  • 49
1

Use some event bus such as Otto

Create an event, make your main activity subscribe to the event using the event bus, start your operation, display a "Loading..." or something ProgressDialog in your main activity. From your worker thread when it completes send an event to your main activity. Make your main activity close the "Loading" dialog when it receives the event,

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • Why would you suggest using an event bus instead of on-board APIs of Android? – Fildor Apr 13 '17 at 07:35
  • Green Robot or Otto are easy to use and there's not much "onboard" APIs for the same (you can use local broadcasts but there are some issues with that) – Alexander Kulyakhtin Apr 13 '17 at 07:38
  • What about AsyncTask? My feeling is that this would be enough for the requirement of the OP, no? Alternatively, I've seen solutions using Loaders. Don't get me wrong, I am not against your suggestion. I just want to understand, why a 3rd party is a necessity here. I can imagine that it would be the easier approach if you are already using an event-bus in your App anyway. – Fildor Apr 13 '17 at 07:50
1

I guess a better question would be why you want to force it on the main thread?

As far as I know, volley won't let you do that but you might be able to if you make your own network operation. After Honeycomb, you will get a NetworkOnMainThreadException so you will need to override the policies.

Community
  • 1
  • 1
Kia
  • 124
  • 1
  • 1
  • 10