Activity identity

DeerBear

Member
Licensed User
Longtime User
Hello!

I am creating a class called CAndroidApp.
Among the other things, I am putting in this code:

B4X:
'Save activity instance
Dim CurrActivity As Activity
...
'This has the same name, but it's fine because it is in another class
Sub StartActivity( AnActivity As Activity )
  If CurrActivity <> AnActivity Then
    CurrActivity = AnActivity
  End If
  StartActivity( AnActivity )
End Sub

Now this code is fine in this instance because, well, if the pointer location of the activity has changed, we need to update it, but I would also like to
check whether the Activity object is of the same type.
Saying it in another way:

Imagine you have a "main" activity.
Then you have a "second" activity.
Then you have a service.
Now from the service, you need to switch
activity but only if the one you want is not current.

The equal sign above will only check for pointers,
(memory locations) which may change if an activity is
destroyed and recreated.
Thus, I still need a way to tell if it's the same activity :)

Anybody has suggestions? :)

Thanks.

A
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your question touches several complex concepts.

The Activity object is different than the Main module object (or any other Activity module object).

In order to start an activity you need to use the Main object not the inner UI Activity object.

For example:
B4X:
StartActivity("Main") 'you can pass a string
'Or
StartActivity(Main) 'or a reference to the module object

You can use the Me object to pass a reference to the module object. This doesn't work in v1.95 but will work in newer versions:
B4X:
'Class1 module
Sub Class_Globals
 Private TargetModule As Object
End Sub

Sub Initialize(Target As Object)
 TargetModule = Target
 StartActivity(TargetModule)
End Sub

'***************************
'Activity module
Dim c As Class1
c.Initialize(Me)
 
Upvote 0

DeerBear

Member
Licensed User
Longtime User
Your question touches several complex concepts.

Not scared by complexity :)

Let me expand a bit on what I wish to do, which will
probably make things a notch easier to understand.

What I want to do is, essentially, a singleton class that gets
instantiated in the main activity.
This class will keep track of what activity is currently running
plus several other informations inferred from this very thing,
such as device orientation, plus will interact with one or
more services to gather other information.
This is being done to keep all of the relevant "status"
information in one place.

The Activity object is different than the Main module object (or any other Activity module object).

Mind to expand on this a bit, please?

In order to start an activity you need to use the Main object not the inner UI Activity object.

For example:
B4X:
StartActivity("Main") 'you can pass a string
'Or
StartActivity(Main) 'or a reference to the module object

You can use the Me object to pass a reference to the module object. This doesn't work in v1.95 but will work in newer versions:
B4X:
'Class1 module
Sub Class_Globals
 Private TargetModule As Object
End Sub

Sub Initialize(Target As Object)
 TargetModule = Target
 StartActivity(TargetModule)
End Sub

'***************************
'Activity module
Dim c As Class1
c.Initialize(Me)

Are you saying that my StartActivity code should really read

Main.StartActivity(AnActivity) ?

I am wondering because, as far as I could see until now,
StartActivity works like a charm from all of the
activities I've used it into :)
Then, if you do some "black magic" I don't know, as
long as it works it's fine with me :)

The reason I want to check which activity is running is
important to make sure that, for example, if a service
receives a request for a picture and the shooting
activity is already running, it doesn't start it again.
Should never happen, but we all know what has been of
what should have never been... :)

Once I know that all is good, then start the activity.

Get my point?

Regards,

A
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is a much simpler way to accomplish it.
You can have a process global string variable in the service. In Activity_Resume of each activity add this code:
B4X:
MyService.CurrentActivityName = "Activityx"

You will then be able to always check the value of MyService.CurrentActivityName.

With that said it is not really necessary in the case you describe.
You can call IsPaused(ShootingActivity) to test whether the ShootingActivity is running or not.

The best option is to use CallSubDelayed. It will start the activity if necessary and run the specified sub.
 
Upvote 0
Top