Suppose I have one activity , MainActivity and one service MyService in my android studio project.
MainActivity.class
Toast.makeText(this,"Point 1",Toast.LENGTH_SHORT).show();
startService( new Intent(this,MyService.class) ) ;
Toast.makeText(this,"Point 3",Toast.LENGTH_SHORT).show();
MyService.class
Toast.makeText(this,"Point 2",Toast.LENGTH_SHORT).show();
The flow of execution is Point 1 -> Point 3 -> Point 2 & not 1->2->3
Since service runs in main ui thread only , so what actually happens in background ? since all 3 points execute on same thread .
EDIT:
What can I do to make execution like 1->2->3 ?
Using a static variable in MyService(and setting a value there) and importing that variable in MainActivity to check if service has been started successfully ,doesnt work( causes ANR- reason: Starting .MyService ).
In MainActivity
while(! staticvar.hasBeenSetInMyService){ }
It gets stuck in endless while loop . So is the static variable never updated in MainActivity or MyService's onStartCommand() doesnt get executed ??