Android Question Wait for Activity to finish before returning to previous Activity

Patrick Clark

Active Member
Licensed User
I am having a major problem trying to control the flow of an app.

I have a Login Activity (actInitialLogin) and a Second Activity (actFirstScreen) that should only be displayed after the Login has completed.

How do I wait for the Login to complete before stating actFirstScreen

using
B4X:
StartActivity(actInitialLogin)
StartActivity(actFirstScreen)

causes actFirstScreen to be displayed and actInitialLogin is never seen (it is on the stack as pressing "back" displays it)

I have tried CallSubDelayed but I cannot get anything to work.
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
I think you want to return to actFirseScreen after the user has logged in; in this case you should call Activity.Finish from within the event routine of the actInitialLogin close button (bntOk_Click, in which you should authenticate the user - or, better, call a routine which authenticates the user).

However, if they were any two "generic" Activities, you should starts one from Activity_Pause of the other one:

Sub Activity_Pause(UserClosded As Boolean)
If UserClosed Then StartActivity(actFirstScreen)
 
Upvote 0

Patrick Clark

Active Member
Licensed User
Hello again

I am still confused.

If I start actFirstScreen and then actInitialLogin

once actInitialLogin completes it returns to actFirstScreen (correct)

But...

The next action in the app is to load actStockCheck (there is no user interaction)

I hit exactly the same problem as before, actStockCheck gets displayed before actInitialLogin completes.

I cannot understand how I can wait for actInitialLogin before continuing the app.

actFirstScreen is like a control hub which will call a number of activities and then pass control to the main app.

each of the activities called by actFirstScreen can also be called later individually
 
Upvote 0

DavideV

Active Member
Licensed User
Longtime User
think about using some events in your app... the login should generate a result, the code validats it, if correct you'll start the next activity...
 
Upvote 0

Patrick Clark

Active Member
Licensed User
Thanks for the reply but I don't understand what event I should be waiting for. Could you give a short code snippet that I can look at that explains how I can create an event that the main process can wait for and the sub-process can fire.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
This is not the only way (or even the best way) but I generally store user credentials in a variable or type variable in my starter service -- that way I can access it from any activity (Starter.UserID for example). In each activity that is "secured" I check that variable and if it is not valid I stop the activity and start the "login" activity.
 
Upvote 0

Patrick Clark

Active Member
Licensed User
This is not the only way (or even the best way) but I generally store user credentials in a variable or type variable in my starter service -- that way I can access it from any activity (Starter.UserID for example). In each activity that is "secured" I check that variable and if it is not valid I stop the activity and start the "login" activity.

I am doing that but I still have the problem with subsequent activities not being able to start until prerequisites have completed.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
As I said, each activity must check the perquisites and invoke the necessary activity to meet them as needed. For example (pseudo code)
B4X:
Private Sub Activity_Resume()
    If Starter.UserId = "" Then
        StartActivity("Login")
    Else
        Callsub(Me, "Continue")
    End If
End Sub

The "login" activity just sets the necessary Starter variable(s) and then issues Activity.Finish. The OS will return control to the activity that invoked the "login" activity (as if the user had clicked the BACK button).
 
Last edited:
Upvote 0

Patrick Clark

Active Member
Licensed User
But then I need to start the next activity in the process which could be started in "Continue" and when that is complete start the next and so on.

So I will me looking at something like this

B4X:
Private Sub Activity_Resume()
    If Starter.UserId = "" Then
        StartActivity("Login")
    Else 
         If Starter.SealChecked = False Then
                StartActivity("SealCheck")
         Else
                If Starter.StockChecked = False Then
                        StartActivity("StockCheck")
                End If
        End If
    End If
End Sub
Or something like that?
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Depends on your design methodology and how your app is structured. If you have a multi-step process (something like a Windows "wizard" interface, I guess) you could just have the MAIN activity handle which activity needs to be displayed based on the items you need "filled in."

For example you could have a select case in your main resume sub (again, in pseudo code):
B4X:
' …
Select Case True
    Case Starter.UserID = ""
        StartActivity("Login")
    Case Starter.SealChceked = False
        StartActivity("CheckSeals")
    ' … and so on
    Case Else
         ' display the "you're done" screen with a "start over" button or something.
End Select

Or, if the items are not inter-dependant, you could have one activity with multiple "tabs" that the user can navigate and then validate them all at once and bring to foreground any tab that has something incorrect, and so forth until all is acceptable.

Again, it really depends on how you wish to implement the UI and what requirements you need for the various steps.
 
Upvote 0

Patrick Clark

Active Member
Licensed User
@Jeffrey Cameron Thanks for your help. I did it with this

Edit: Oops posted this before I read your last post - Thanks again

B4X:
Sub Activity_Resume
    Select True
        Case act1=False
            StartActivity(act01)
            Log("Act1")
        Case act2=False
            StartActivity(act02)
            Log("Act2")
        Case act3=False
            StartActivity(act03)
            Log("Act3")
        Case Else
            Log("All Done")
            ExitApplication
    End Select
End Sub
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Please note: The OS will treat your app as if it has crashed when you call ExitApplication. In some cases it will try to restart it automatically.
 
Upvote 0
Top