Android Question Get Activity from Code Module

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am running a Code module and inside the Code Module I have a Sub like this:

CodeModule:
B4X:
Sub OpenActivity(CurrentActivity As Activity)
    StartActivity(Activity2)
    CurrentActivity.Finish
End Sub

On the main Activity I am running the code from a button and when I press it I am doing the following:

B4X:
Sub Button1_click
    CodeModule.OpenActivity(Activity)
End Sub

Based on the above it works fine, and opens the activity and closes the previous activity which is what I want.

However, if I add a button to Activity2 and run the code:
B4X:
CodeModule.OpenActivity(Activity)

It then seems to hide the app. (well hides the activity).

The part I need help with is somehow from the Code module to detect if CurrentActivity is the activity that is going to be open and not to run the code.

For Example:

CodeModule:
B4X:
Sub OpenActivity(CurrentActivity As Activity)
    'If CurrentActivity = "Activity2" Then Return '<< Need help with this line
    StartActivity(Activity2)
    CurrentActivity.Finish
End Sub

Anyone know how to stop the code based on the Activity name?
 

aaronk

Well-Known Member
Licensed User
Longtime User
I think I might need to do something like:

B4X:
Sub OpenActivity(CurrentActivity As Activity, ActivityString As String)
    If ActivityString = "Activity2" Then Return
    StartActivity(Activity2)
    CurrentActivity.Finish
End Sub

If I then run the code on Activity2 I can run it as:

B4X:
CodeModule.OpenActivity(Activity,"Activity2")

Just didn't know if there was a better way or not.
 
Upvote 0
Top