Android Question [b4a] How to find current activity (if any) from a service

AscySoft

Active Member
Licensed User
Longtime User
On firebase service i need to pass some data from a new message received to currently running activity if any, if not to Main activity (callsubdelayed2)
I did tried this: https://www.b4x.com/android/forum/t...ctivity-from-a-code-module.68666/#post-471623, no luck
Log says: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference

I intend to implement just a sub with a simple MsgBox function, but I have no idea how to code this once and display in any of the many posible running activity, except creating this sub in all the activities.
Your help is appreciated.
 
Last edited:

JordiCP

Expert
Licensed User
Longtime User
Hi,

No need to know which is the current running activity.
Just create a new one which just shows the message and closes itself later.
B4X:
#Region  Activity Attributes
   #FullScreen: False
   #IncludeTitle: False
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
End Sub

Sub Activity_Resume
   Msgbox("Hello","I am the message activity")
   Activity.Finish
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Let's suppose you have named it "msgActivity". Then add this to your manifest editor so that it will have transparent background.
B4X:
SetActivityAttribute(msgActivity, android:theme, "@android:style/Theme.Translucent")
From your service, just call StartActivity(msgActivity), and that's all :)
 
Upvote 0

AscySoft

Active Member
Licensed User
Longtime User
create a new one which just shows the message and closes itself later
This is not what I want because it will brake whatever users are doing. For example a user is actively involved in creating customers orders, in tracking products delivery and so on...so the ideal way is not to intrerupt the current user activity to much. But thanks for your idea and for your answer.
 
Upvote 0
Top