Android Question CallSubDelayed on fm_MessageArrived

sinuser

Member
Licensed User
Longtime User
Hello to all,
I need to solve this synchronization problem:
when the fm_MessageArrived event intercepts the notification I have to fill a grid.
The problem is if the app is closed or in the background the app crash.
I use the CallSubDelayed to call the main method that fill the grid but obviously if the activity has not finished loading the app crash.
Now in the main method called i use this:
Do While Not (ApplicationIsReady)
Sleep (1000)
Loop
that works but I do not like it, is there a better method?
Thanks
 

sinuser

Member
Licensed User
Longtime User
the app crash because the activity is not created, these are my methods:

Sub fm_MessageArrived (Message As RemoteMessage)
Try
Dim ComunicationID As Int = Message.GetData.Get("ComunicationID")
Dim m As String = Message.GetData.Get("body")
Dim t As String = Message.GetData.Get("title")
Dim n As Notification
n.Initialize
n.Icon = "icon"
n.AutoCancel = True
n.SetInfo(t, m, Main)
n.Notify(1)
CallSubDelayed2(Main, "OpenComunication2", ComunicationID)
Catch
Log(LastException)
#if DEBUG
Msgbox(Message, "Notifica")
#End If
End Try
End Sub

private Sub OpenComunication2(ComunicationID As Int)
Do While Not(ApplicationIsReady)
Sleep(1000)
Loop
Dim cm As ResumableSub = RefreshData
ProgressDialogShow2("Aggiorno...", False)
Wait For (cm) Complete (Result As Boolean)
ProgressDialogHide
If Result Then
Dim c As Comunication = CurrentUser.GetComunucation(ComunicationID)
OpenComunication(c)
Else
Msgbox("Errore di connessione, provare ad aggiornare i dati!","Attenzione")
End If
End Sub


with these rows:
Do While Not(ApplicationIsReady)
Sleep(1000)
Loop
the app don't crash but i dont think is the right method to do this no?

Thanks
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
It's not a good method to start an activity from a (background)service. Better:

1. When new data arrives, store the list with maps e.g. in a file using RAF (Random Access File)
2. Throw a notification like "new data for you"
3. When the user opens the app/activity/notification load and display the file's content as you mentioned

Remember that Android is always "USER-driven". Services are good for simple and non-ui tasks. All UI-things are under demand of the user. If you follow this, you won't have any problems with higher versions of Android in the future.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…