Android Question Prevent app to run until some code has been executed?

Wulf

New Member
Hi!

I have searched and read tutorials but I couldn't find an answer to my question. I have problems to understand the correct use of "Wait For". I know, what it does. But I have no idea, how to write "clean apps" with this command for my case ...

This is my program (extremly shortened)::
Sub AskForPermission
   rp.CheckAndRequest (rp.PERMISSION_READ_PHONE_STATE)
   Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
End Sub

Sub Activity_Create(FirstTime As Boolean
   If FirstTime = True Then
      AskForPermission
   End If

   Do_More_Things
End Sub

I want to wait, until the Permission-Check has finished. Do_More_Things() should be executed, after AskForPermission() is completed. I don't want, that my app will continue before that.
So, now I can do the following::
Sub AskForPermission
   rp.CheckAndRequest (rp.PERMISSION_READ_PHONE_STATE)
   Wait For Activity_PermissionResult (Permission As String, Result As Boolean)

   CallSubDelayed(Me, "Sub_is_finished")
End Sub

Sub Activity_Create(FirstTime As Boolean
   If FirstTime = True Then
      AskForPermission

      Wait For Sub_is_finished
   End If

   Do_More_Things
End Sub

Now, Activity_Create starts and calls AskForPermission. AskForPermission runs and come to "Wait For Activity_PermissionResult" and exits AskForPermission (because it waits now). So we are back in Activity_Create and come to "Wait For Sub_is_finished". Because this event didn't happen, the Sub is also exited and the app is continued (with Activity_Resume). But I want to wait (app should be blocked because the app is useless without the asked permission).
I know a solution::
Sub AskForPermission
   rp.CheckAndRequest (rp.PERMISSION_READ_PHONE_STATE)
   Wait For Activity_PermissionResult (Permission As String, Result As Boolean)

   bol_Sub_is_finished = True
End Sub

Sub Activity_Create(FirstTime As Boolean
   If FirstTime = True Then
      AskForPermission

      Do
         If bol_Sub_is_finished = True Then Exit
         DoEvents
      Loop
   End If

   Do_More_Things
End Sub

Is this the recommended solution? I have no problem with this, but I would like to know, if this is the recommend way to stop execution of an app, until some things were finished before? Does it cost much battery (because the Do/Loop repeats and repeats and repeats very often and I think, this cost CPU power and drains the battery)?

Thank you very much!!
Wolfgang
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. The only thing that you should do with this DoEvents code is to delete it.
2. The only thing that you should do with the CallSubDelayed code is to delete it.
3. Don't waste your time with activities. Switch to B4XPages. I'm familiar with B4A for 10 years+ and I know what I'm talking about.
4. If you mistakenly want to implement the permission related code in a different sub then you should make it a sub that returns a ResumableSub and follow this tutorial:
[B4X] Resumable subs that return values (ResumableSub)
 
Upvote 0

Wulf

New Member
Thanks Erel for your hint (B4XPages). Now I've read some tutorials and watched youtube-clips about that. Now I think I know, what you mean. ;-)

Regarding my question to prevent an app running until the user give his permission:
Is this the right way to work with Resumable Subs, when my app may only be continued, if the user give the permission? ...:
1.) User starts my app
2.) At the beginning: all views on my UI are disabled
3.) My app is waiting for the permission (Wait For Activity_PermissionResult...)
4.) After the "Wait For" Statement, I will enable my views.

Is this a recommend way?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…