Android Question What could be the ways to discover what starts an Activity?

LucaMs

Expert
Licensed User
Longtime User

ilan

Expert
Licensed User
Longtime User
first the Starter service will start and then the main activity.

no other activity should start if you dont start it with "startacitivity"

the cycle should be like this

> starter (service)
> main (activity)
- activity_create
- activity_resume

if u exit your activity via home button then only activity_resume will be fired (when you return!)
if you exit your app via back key or activity.finish then activity_create will be fired first then activity_resume (when you return!)

look for command (startactivity) in all acitivity_resume and activity_create subs
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Well, I know "enough" :) the App and Activities life cycle.

look for that command (startactivity) in all acitivity_resume and activity_create subs
Already done. StartActivity is executed once only, but the Activity restarts right after its closing.
Other "things" can start an Activity, like a call to one of its routines, using CallSubDelayed.

However, I "logged" all code lines executed, as you can see in that thread, but without success.

I was hoping to be able to understand something even from unfiltered log, but to no avail.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
can you post the activity_pause sub of that activity that restart after closing?
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    ActivityIsStarted = Not(UserClosed)
End Sub

ActivityIsStarted is a boolean variable; it can seems to be suspect. It's not so simple to explain how I use it, I try.

Right because of that error, I thought it would due to two CallSubDelayed which are in a service module and call an Activity's routine.
The log shown that, sometimes, the first execution can start when the Activity is in background but the second one when the Activity was in foreground. So I used a timer in the service, to check if the Activity is in foreground (better, if it was resumed), and when it is, I execute all (two only, at that moment) CallSubDelayed.

B4X:
' TODO commentare bene!
Public Sub rtnFromServerPlayerData(Params As List)
    mlstDataArrivedForActGameRoom.Add(Params)
    If Not(IsPaused(actGameRoom)) Then
'        CallSubDelayed2(actGameRoom, "PlayerDataArrived", Params)
        PassDataToActGameRoom
    Else
        StartActivity(actGameRoom)
        tmrActGameRoomReady.Enabled = True
    End If
End Sub

' TODO commentare bene!
Private Sub tmrActGameRoomReady_Tick
    If actGameRoom.ActivityIsStarted Then
        tmrActGameRoomReady.Enabled = False
        PassDataToActGameRoom
    End If
End Sub

' TODO commentare bene!
Private Sub PassDataToActGameRoom
    Do While mlstDataArrivedForActGameRoom.Size > 0
        Dim Params As List = mlstDataArrivedForActGameRoom.Get(0)
        CallSubDelayed2(actGameRoom, "PlayerDataArrived", Params)
        mlstDataArrivedForActGameRoom.RemoveAt(0)
    Loop
End Sub

"TODO commentare bene!" means: remember to add a good comment :D
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
how do you exit that activity?
B4X:
Private Sub btnExit_Click
    Dim Answ As Int
    Dim txt As String
    txt = "Exit from room: are you sure?"
    If mStatus = STATUS_PLAYING Then
        txt = txt & CRLF & CRLF & "IF YOU EXIT NOW, YOU WILL PAY [xxxx]"
    End If
    Answ = Msgbox2(txt, "ATTENTION", "Yes", "", "No", Null)
    If Answ = DialogResponse.POSITIVE Then
        RemovePlayer(mMyTurnOrder)
        mMyTurnOrder = -1
        CallSubDelayed2(srvComm, "RunOnServerPlayerExitsGameRoom", True)
        LogColor("Chiudo actGameRoom", Colors.Red)
        Activity.Finish
    End If
End Sub

B4X:
Private Sub RemovePlayer(TurnOrder As Int)
    Dim Player As clsPlayer = mmapPlayers.Get(TurnOrder)
    mPlaces(Player.PlaceIndex).GetLabelTop.Text = "Vacancy"
    mPlaces(Player.PlaceIndex).Photo = mDefaultPhoto
    Player = Null
End Sub

mPlaces is an array of custom views. Inside of RemovePlayer I use an Activity's View (a label, from GetLabelTop) but the line:
B4X:
RemovePlayer(mMyTurnOrder)
should be executed "instantly", not delayed, or am I wrong? Well, I have just to try, commenting that (those) line.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
if you call a sub in a different activity should it not close anyway your active activity?

try to remove activity.finish
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
if you call a sub in a different activity should it not close anyway your active activity?
This cannot be the problem, because "finish"ing an Activity cannot restart it ;).
Also, the current Activity is not finished, in that case, but paused, with UserClosed = False, and it isn't that Activity that is restarted "automatically": Main starts the actGameRoom and this one is restarted.

However, the "bad" Activity (actGameRoom) is started from an Activity (Main, "first time", when the user selects it from a list) and from a Service (when data are received from a server, as you can see in #7).
Both cases was checked; also, if the error was there, I would see the executed lines in my "special" logs (all lines that will be executed are "logged"; as example, all server's incoming calls are logged).
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Do you receive something putting in your Resume sub ?
B4X:
Activity.GetStartingIntent
I explained badly in that post, sorry; it was:
if you call a sub in a different activity should it not close anyway your active activity?
This cannot be the problem, because "finish"ing an Activity cannot restart it ;).
Also, the current Activity is not finished, in that case,
with "that case" I meant the case Ilan mentioned (I'm explaining very badly even now :D).
I meant that if you call an Activity's routine from an Activity, the current Activity will be only paused, but this is not my case or, better, it is not the "starter" Activity that will be accidentally-automatically restarted, but the "started" Activity.


However, this can be very useful (and I don't knew it!!! I knew Intents and also their ExtraData info, but I don't knew that their were even in B4A's Activities).
I try immediately :)


Thank you, Lemon
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Forse l'ho trovato :D (please wait for the translation or use Google Translation :p)...
Maybe I found it :D:mad::)

B4X:
TextMsg :
    (ArrayList) [[1, Phone, (omissis)
Message longer than Log limit (4000). Message was truncated.
[srvComm] code...Public Sub rtnFromServerPlayerData(Params As List)
code...mlstDataArrivedForActGameRoom.Add(Params)
code...If Not(IsPaused(actGameRoom)) Then
code...StartActivity(actGameRoom)
code...tmrActGameRoomReady.Enabled = True
code...End If
TextMsg :
    (ArrayList) [[0, Tablet, (omissis)
Message longer than Log limit (4000). Message was truncated.
[srvComm] code...Public Sub rtnFromServerPlayerData(Params As List)
code...mlstDataArrivedForActGameRoom.Add(Params)
code...If Not(IsPaused(actGameRoom)) Then
code...StartActivity(actGameRoom)
code...tmrActGameRoomReady.Enabled = True
code...End If
** Activity (main) Pause, UserClosed = false **

TextMsg if the message received from server. Since I used this method #7 I check each time if the Activity is paused and... if not, I start it again!

It should be noted that I have found the error thanks to my new AddLogs :rolleyes: (but I would not have had to do that error :mad:).


Thank you all for putting up with me.
 
Last edited:
Upvote 0
Top