Android Question Problem with Activities / Program flow

Andy Whitehouse

Member
Licensed User
Longtime User
Hi all.

I'm desperately looking for help.
I have read the forum and the user guides and I cant see that anyone has encountered this before.

I am a newbie at B4A (but a Whizz at Qbasic and OK at Visual Basic) and am having major problems in controlling / losing program flow control when using user input (buttons), layouts but, more particularly, starting new activities.
Some activities just do not start at all when called (I've even called the activity several times one after the other) or others "start", but pass control back to the next line of the programme after the start request, and then only display the second activity when the rest of the code has run (which makes it pointless as the code needs the user data to continue to the next part).
This happens even if I have used activity.finish to close the previous activity
I have even got one programme which jumps to code in a code module after an activity has been called when the module has definitively not been called.

Sometimes, when the activity starts, it returns to the main module without any user input even when the code is controlled by conditions (not always activity.resume)

A sample code is attached (a much trimmed down version of my programme) just to see if others have the same experience. Basically, on this code, the second activity is only displayed once the rest of the code has completed, despite it being called mid way.

I am using the Google AVD emulator, the latest Android SDK (API 19 and 20 both tried), the latest Java download and the latest version of B4A. I didn't know if this was a problem with my installation so I have installed all on a second computer and getting the same results.

All other programming code flows and works fine - its just when I call layouts (sometimes) or start second activities.

I have tried using callsub delayed. do while ... etc.etc. but to no avail.

Is this normal (intended) programming behaviour or is this a problem?
Or is it something where I have no choice but to code for all of the outcomes via activity.resume (if I don't use activity.finish then often, but not always, the programme goes back to activity.resume)

Thank in advance - really appreciated.
If I'm missing something then always willing to listen

Andy
 

Attachments

  • Testprogram1.zip
    73 KB · Views: 124

klaus

Expert
Licensed User
Longtime User
I'm afraid that you haven't understoud the program flow and activity life cycle.
Unfortunately you don't explain what you want to do.

B4X:
Sub NewPersonButton_Click
    StartActivity("Displaydateandtime")
    Log ("It comes back without a key press")
    Activity.LoadLayout("blanklayout")

NewNatalSubject.Main_TaskNewNatalData

End Sub
In this code you start a new activity, which means that the current activity is paused !
Activity.LoadLayout("blanklayout") will have no effect because the current activity will be closed.
When StartActivity("Displaydateandtime") is called in Sub NewPersonButton_Click the routine doesn't wait the return of the called activity but continue executing the code after the statement.
It starts the new activity and executes the following code just after, B4A is event driven.

Can you please explain in detail what you want to do and what you expect and we will give you concrete advices.
 
Upvote 0

Andy Whitehouse

Member
Licensed User
Longtime User
Hi Klaus,

Thanks for the response. I had read the user guides and I couldn't find the solution which worked.

However, I have re - read the process life cycle and think I know where I am going wrong.
A simple yes or no from you should do the trick. I also explain my wrong thinking below as this may help others.

Firstly, What I am trying to do is pretty simple really - (and my programme concept IS event driven):-

a) Screen Display to user with several buttons; user chooses option which drives program flow
b) Get a user input (date, name, location), wait for user entry (this is where I am having a big problem) , and pass that result back to code for processing (please see below).
c) Display results of processing
d) Give user further options as to what they want to do next (e.g. return to main screen).

I am writing an astrology programme where there are several modules which could rely on a user input (e.g. a date) and rely on the result, and have been treating the user input as a sub routine rather than the driver. I am pretty sure this is where I am going wrong.

I had the (misconception?) that an Activity module should just be used for user input / display / "activity"; little other general code should be in there, and also that an activity had limits on what other parts of the code it could access. However, I have re - read the user guides and have found nothing which supports this - I think I had misread the user guides originally. I also had the preconceived idea that "Main" was a hybrid of an activity and a code module which I no longer believe is the case (My new understanding is that "Main" is simply the core starting activity from which everything else flows).
My new understanding is that "activity" is "king". i.e. THE ACTIVITY calls the other code modules, not the code modules calling the activity, which is what I have been doing.

If you can confirm this point to me (and I am working away testing that right now) then I think the problem may be solved.



Thanks

Andy
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
... "Main" is simply the core starting activity from which everything else flows.
Yes.
THE ACTIVITY calls the other code modules, not the code modules calling the activity, which is what I have been doing.
Yes.

Did you have a look at chapter 13.2 Program with 3 Activities in the Beginner's Guide ?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…