Android Question B4A App Freezes on First HTTP Request After Install (Works After Restart)

floruit

New Member
Hi all,
I’m fairly new to B4A and Android development, and I’m stuck on an issue that I can’t quite reason through yet. I’ve searched the forum but haven’t found a case that matches this exactly.

The problem​

On a fresh install of my app, the very first HTTP request causes the app to freeze for a few seconds, and sometimes it triggers an ANR. If I force-close the app and open it again, everything works perfectly after that.

Environment​

  • B4A version: 13.x
  • Android target: SDK 34
  • Device tested: Pixel 6 (Android 14) and emulator
  • Libraries: OkHttpUtils2, JSON, Core

What the app does​

  • App starts → shows a login screen
  • On login button click, I make a POST request to my server
  • Server responds quickly (confirmed via logs)

Relevant code (simplified)​

Sub btnLogin_Click
Dim job As HttpJob
job.Initialize("login", Me)
job.PostString(ServerUrl & "/login", $"username=${txtUser.Text}&password=${txtPass.Text}"$)
End Sub
JobDone fires correctly after the freeze, but on first run only.

What I’ve tried​

  • Moving the request to Starter service
  • Adding a short Sleep(0) before the request
  • Checking permissions (INTERNET is present)
  • Disabling cleartext traffic issues (using HTTPS anyway)

Questions​

  1. Is there something about first-run initialization (OkHttp, TLS, DNS?) that I should be handling differently?
  2. Do I need to warm up OkHttp or run the first request in a service instead of an activity?
  3. Is there a common B4A pattern to avoid ANR on the very first network call?
Any guidance or best practices would be appreciated. I’m still learning the “B4A way” of structuring apps, so feel free to point out obvious mistakes too.
 

josejad

Expert
Licensed User
Longtime User
JobDone fires correctly after the freeze, but on first run only.
Not sure if this is the problem, but JobDone is not needed. Take a look to:
 
Upvote 0
Top