Bug? StartActivity(Main) works in service but not class

qsrtech

Active Member
Licensed User
Longtime User
I'm revisiting my Push Library I was working on some time ago. I "quit" because I was having an issue raising events when the activity holding the class instance was not "active". I'm starting to make some progress however when I try StartActivity(Main) from within the class it doesn't work like it does from within a service.

Here's my service code:
B4X:
Sub MessageArrived (Intent As Intent)
    Dim From, CollapseKey, data As String 'ignore
    If Intent.HasExtra("from") Then From = Intent.GetExtra("from")
    If Intent.HasExtra("data") Then data = Intent.GetExtra("data")
    If Intent.HasExtra("collapse_key") Then CollapseKey = Intent.GetExtra("collapse_key")
    For Each aPush As Push In PushGlobals.PushList
        If aPush.ProjectID=From Then
            StartActivity(apush.Activity) 'THIS WORKS
            aPush.MessageRecieved(data)
            Exit
        End If
    Next
End Sub

Here's my Class Code which ultimately raises the event:
B4X:
Sub MessageRecieved(data As String)
'raise an event
    If SubExists(target,Event & "_" & "Message") Then
        StartActivity(Activity) 'THIS DOES NOT WORK
        CallSubDelayed2(target,Event & "_" & "Message",data)
    End If
End Sub

Also, is there a way to call a code module sub without knowing the sub before hand? For example, in this situation, we may not always want the activity to become active, which would be the only way to run the "event" code. I'd like to pass a "code modules" sub to the class instance to be called upon a message. For example:
B4X:
Dim APush as Push
APush.CodeToCallOnMessage="Globals.MyPush"

Then within the class's "MessageReceived" routine I can call the sub:
CodeToCallOnMessage(data)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

qsrtech

Active Member
Licensed User
Longtime User
Yes. You should implement an "event" mechanism as done in many of the classes.

Here is one example: [Class] AnotherDatePicker - A simple "web style" date picker

This class/project (ADP) does not have a "code module" in it. I need to be able to call an unknown routine within a "code" module, not a class, activity or service module. As mentioned sometimes a user of the class may not want/need the activity to interrupt the user so they need to be able to have a routine within a "code" module to be designated for the class to call upon receiving messages.

Further testing indicates StartActivity does not work from within a code module as well.
 

Attachments

  • StartActivityTest.zip
    8.3 KB · Views: 221

Erel

B4X founder
Staff member
Licensed User
Longtime User
Code modules do not hold any context of their own. Therefore they cannot handle events or CallSub calls. You should instead use a class or service.

I've tried the project you attached. I pressed on the Start button and then on the Home button. After 10 seconds the main activity is started.

I guess that you see a different behavior, right?
Make sure that the process is not killed while it is in the background.
 

qsrtech

Active Member
Licensed User
Longtime User
Code modules do not hold any context of their own. Therefore they cannot handle events or CallSub calls. You should instead use a class or service.
I can get a "Code" module routine to run when called from the class "hard coded". For example it creates a notification in the background.

I've tried the project you attached. I pressed on the Start button and then on the Home button. After 10 seconds the main activity is started.

I guess that you see a different behavior, right?
Make sure that the process is not killed while it is in the background.

You have success with the check box both checked and un-checked? Which device?

I'm quite perplexed. It (Class starting Activity, i.e. checkbox unchecked) doesn't work on a Note2 phone, Lenovo 7" tab or Bluestacks emulator. I changed the Class variable in the activity to be a Global variable vs process_global and here's what happens. The first time it pops the msgbox when I use recent apps key, on the second try it has "Initialized" error so the class instance variable still seems to hold valid data the first time, but not the second time. Other findings:
When calling StartActivity from service the Activity_Create is not re-called, whereas when I bring the app forward again via the recent apps key Activity_Create is called again and FirstTime=False. Note Activity_Create is only called again from recent apps key if the timer tried to start the activity from the class. When the app is in the background without the timer running, launching it via recent apps DOES NOT run Activity_Create again.

In summary, StartActivity from within Service does not call Activity_Create whereas StartActivity from within class does, and only when the app is brought up again via the recent apps, which is kind of odd since the call to the class to start the activity is called from the service.

EDIT: I had the test app running (sitting in background) in the emulator for 12 hours today and when I brought it forward it did not run the Activity_Create event.
 
Last edited:
Top