Android Question Call a Sub in Activity from a Service

MarkusR

Well-Known Member
Licensed User
Longtime User
hello,
i have a little problem, if me call a sub (in an activity) from a service and the activity is not in front
it seems it create a new activity instead of open the one that is in pause mode.
 

DonManfred

Expert
Licensed User
Longtime User
it seems it create a new activity instead of open the one that is in pause mode.
it is recreated if android has killed it already. Yes.
If it was still in background then activity_create is called with Firsttime set to false.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
it is recreated if android has killed it already. Yes.
If it was still in background then activity_create is called with Firsttime set to false.

but if me click back from second activity its there and unchanged.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i used CallSubDelayed2 from own service
B4X:
Private Sub Client_MessageArrived(Topic As String, Payload() As Byte)
    
    Dim received As String = BytesToString(Payload,0,Payload.Length,"UTF-8")
    Dim m As Message
    m.Initialize
    m.Topic = Topic
    m.Body = received
        
    CallSubDelayed2(Main,"AddMessage",m)
        
End Sub
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
You shouldn't end with two instances of the same activity.
me expected it used this one, means awake if other activity is in front.

nothing special in manifest.
here the log from debug mode, it really killed the previous activity. (this phone is android 5.0.2)
i switched via menu from main to activitycontrol, there i clicked a button which send data via service to mqtt broker.
the mqtt broker answer to my connection service and this should show in main activity.
Logger verbunden mit: motorola XT1032
--------- beginning of main
Copying updated assets files (5)
*** Service (starter) Create ***
Starter.Service_Create
** Service (starter) Start **
Starter.Service_Start
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (connection) Create ***
Connection.Service_Create
** Service (connection) Start **
Connection.Service_Start
Connection.StatusToActivity Service Start ..
PhoneEvents1_ConnectivityChanged
Connection.Connect
Connection.StatusToActivity Connect ...
Connection.Client_Connected: true
Connection.StatusToActivity Connected
** Activity (main) Pause, UserClosed = false **
** Activity (activitycontrol) Create, isFirst = true **
** Activity (activitycontrol) Resume **
Connection.AlarmOnOff true
Connection.SendMessage
Connection.Client_MessageArrived
Connection.Client_MessageArrived
** Activity (activitycontrol) Pause, UserClosed = false **
Killing previous instance (main).
** Activity (main) Create, isFirst = false **

Manifest Editor
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
This is fine. It can happen in some cases.
its unusual because i have only 2 activitis and if me click back from the other one the main always open.
but i considered to change it that the activity itself fetch this information if needed from service.
 
Upvote 0
Top