Android Question callsubdelayed

John H. Guillory

Member
Licensed User
Longtime User
O.k. I've made work arounds before, and have a work-around in this app, but I just can't help but look at the examples and believe there's a better way.

Activity1 has a screen with a button called bAdd, in bAdd's click event, I want to start activity 2 and call a sub called add_start (could just use the built in activity create event, if I knew there was a simple way to just load an activity if it wasn't already loaded and resume it and make it visible if not.....

So I use callsubdelayed(add,add_start)

But when I do, it says add_start is used before being declared, so even though it's add's module I'm wanting to call the sub from, I have to create an empty sub on activity1 named add_start to use callsubdelayed(add,add_start)

There may be a simple way of doing what I want to do easier, I'm not afraid to admit that android programming is not my forte. What is my forte is program design. I'm used to working out the details and letting the compiler design for the operating system. I love to make applications that run on multiple platforms. That's why I purchased b4a to begin with. It wasn't till I started with b4a that I started to learn more about how android apps are typically organized. Yes, I'm still learning the development, but I can't help but think this is something that could be better, and if so, perhaps it is something that could be better documented in the tutorial and manuals.
 

mangojack

Expert
Licensed User
Longtime User
John , if your second activity is called ' Activity2 ' then ..
B4X:
callsubdelayed(Activity2, "add_start")
.
Also to simply start another Activity then call ..
B4X:
StartActivity(Activity2)

Also .. Have you read Section in Beginners Guide , section 13.2 . Example with 3 Activities.
 
Last edited:
Upvote 0

John H. Guillory

Member
Licensed User
Longtime User
I've done that before, but from what I remember, I thought once you started the Activity, then switched back to the main activity, then called it again it was debateable if it'd cause Activity2 to become foreground again or not... I'm wanting to say that's why I started using callsubdelay. I will admit, it's been a while since I read the Beginner's Guide, I need to check it out again. So eg.

Main, Activity2, Activity3

From Main, I call Activity2, then from Activity2 I call Main, then from Main I call Activity3, then from Activity3 I call Main, then Main Calls Activity2,
all is fine that way? And is there any way to return from an activity to the main activity, or do I have to just StartActivity(Main) ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
normally you start act2 from main. in act2 you should not START main, you should finish act2. You then get back to where you come from... In this case main. I dont know how the app reacts of situations like main->act2->main->act3->main
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You can have different scenarios:
1) You always want to go back to Main.
Call the activities with StartActivity(ActX).
Clicking the Back button in any activity other than Main goes back to Main.

2) From every Activity you want to go back to the calling Activity.
Example: Main > Act2 > Act3 > Act2 > Main
In the example above, you need in Act2 an event to call Act3.
In all activities start the next activity with StartActivity(ActX).
Don't use Activity.Finish elsewhere. Clicking the Back button goes back to the calling activity.
When you click the Back button in Act3, the program goes back to calling activity which is Act2.
If you go from, Main > Act3, and click on the Back button, in Act3, returns to Main which is the calling activity.

3) From every Activity you want to go back to Main.
Example: Main > Act2 > Act3 > Main
In the example above, you need an event to call Act3.
In Act2 you must add Activity.Finish just before or after StartActivity(Act3)
Then when you click the Back button in Act3 you go back to Main.

You can play with the attached example FourActivityExample (it's a modified version of the ThreeActivityExample from the Beginner's Guide).
In Page2 there is an Activity.Finish but not in Page3.
So if go Main(Page1) > Page2 > Page3 > Page4
and you click the Back button you go
Page4 > Page3 > Main(Page1)
You skip page 2 because of the Activity.Finish.
 

Attachments

  • FourActivityExample.zip
    16.1 KB · Views: 160
Upvote 0
Top