Android Question Invoking multiple activities back-to-back w/o an event

clurbina

Member
Licensed User
Longtime User
Newbie question.
I modified the Two Activities example to see if I could learn how to call multiple activities without an event.
I copied Activity2, modified it slightly and named it Activity3.
I was able to call multiple activities by using the click event of multiple buttons.
But how do you call multiple activities without ANY event?
Under certain conditions I want to get data from the user by means of multiple activities.
Of course, I could use panels, but the consensus from my searches indicate that multiple Activities is the proper way.
When I invoke multiple Activities in the code below, it ignores Activity2 and goes directly to Activity3.
From other forum content, somehow this seems right.
However, what is the proper way of calling multiple activities without an event so that every Activity is invoked in the proper sequence?
I've searched the forum a lot but realize that I don't know how to frame the question or the search because what is covered in the content that I found in the searches does not really address this problem.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim DoIt As Boolean=True
    Activity.LoadLayout("1")
    If DoIt Then
        CallSubDelayed2(Activity2, "ShowList", "Activity2 title")
        CallSubDelayed2(Activity3, "ShowList", "Activity3 title")       
    End If
End Sub
 

clurbina

Member
Licensed User
Longtime User
The OS is only ever running one of your activities at a time. Your activity 1 would call activity 2, in activity 2 you have a "next" button which calls activity 3, or back to 1 if cancel, etc.

See: https://www.b4x.com/android/forum/threads/android-process-and-activities-life-cycle.6487/
Thanks for your reply.
I watched that video twice before I posted; I believe you that the OS only runs one activity at a time. But there should be a way for activity 2 to start, execute and finish before Activity 3 starts. Remember, I know how to do this with buttons and their events, but can't it be done without events? I think that I know that there is such a thing called a queue and that these calls to Activities are placed on that queue then executed when the OS decides. Is that the only way? Can't an Activity be "called" like a sub?
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Not sure what you want to achieve, but what about call
B4X:
CallSubDelayed2(Activity3, "ShowList", "Activity3 title")
in Activity2 when you do what you have to do there?
 
Upvote 0

clurbina

Member
Licensed User
Longtime User
Jeffrey,
I decided not to fight city hall and did it the way that you suggested. Thanks again.
As i get more familiar with B4A, i will probably discover the proper way to do it.
For now, your suggestion is fine.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I decided not to fight city hall
It's not a matter of fighting, you have to understand that once you start the 2nd activity, the 1st one is literally no longer running, so there's no place to "go back to" when the 2nd activity is finished.

For example, consider these 2 subs:
B4X:
Sub Start2AndQuit
 StartActivity("Activity2")
 Activity.Finish
End Sub
Sub Start2AndComeBack
 StartActivity("Activity2")
End Sub

If you use Start2AndQuit and in activity 2 you issue a "Activity.Finish" you app will close completely.

If you use Start2AndComeBack and do the same "Activity.Finish" in activity 2 it will re-start Activity 1 (note the re-start, not resume).
 
Last edited:
Upvote 0

clurbina

Member
Licensed User
Longtime User
OK, Jeffrey, I understand better now. What you said about "there's no place to "go back to" when the 2nd activity is finished" is very, very helpful. What we have here is more like a web page that loses state. It's not like a subroutine, where when it is done, it returns to the point of invocation. When you call an activity and you want to return to the point of invocation you have to call that point from the called activity. And that is why
CallSubDelayed2 is used, so you can call an Activity at a specific subroutine.
Anyway, thanks for your further explanation.
I appreciate the helpfulness of the Forum members.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
It's not like a subroutine, where when it is done, it returns to the point of invocation.
Yes, and no... Unlike a VB.NET subroutine, where execution stops after the blocking call, in B4A the code continues to execute PAST your call statement until it hits the end of the procedure or a Return statement. That's why the "Activity.Finish" In my example above is significant and works as described.
 
Upvote 0

clurbina

Member
Licensed User
Longtime User
That is a fine point in your response that I had overlooked. Thank you for emphasizing it. i had noticed that behavior when i stepped through the code with F8. So we need to be careful what code we place after calling an activity because it executes BEFORE the activity is called even though it is placed AFTER the activity is called. Is my understanding correct?
 
Upvote 0
Top