Android Question StartActivity doesn't pause current activity

DrAlex

Member
Licensed User
Longtime User
Hello,

I have a strange phenomenon.

In my program Main activity needs to download a file from a site protected by a username and a password. It starts LogIn activity, which opens a login window and asks the user for a username and a password. After the user enters his username and password, he clicks btnLogin button. Then btnLogin_Click subroutine starts, copies the username and the password into global variables, set the global Boolean variable blnLoginClicked to True and finishes the activity.

Here is the relevant segment of Main activity:

.....................................
Sub ShowLoginScreen
Log("In ShowLoginScreen")
StartActivity("LogIn")
' If "Log In" button was clicked, attempt to download the file.
Log ("In ShowLoginScreen: LogIn.blnLoginClicked = " & LogIn.blnLoginClicked)
If LogIn.blnLoginClicked = True Then
GetFile(strFileName, LogIn.strUsername, LogIn.strPassword)​
End If​
End Sub
...........................................

Here is the relevant segment of LogIn activity:

..............................................
Sub btnLogin_Click
Log ("In btnLogin_Click: edtUsername.Text = " & edtUsername.Text & ", edtPassword.Text = " & edtPassword.Text)
If edtUsername.Text = "" Then
Msgbox("Please enter your username", "Username, please")​
Else If edtPassword.Text = "" Then
Msgbox("Please enter your password", "Password, please")​
Else
strUsername = edtUsername.Text
strPassword = edtPassword.Text
blnLoginClicked = True
Activity.Finish​
End If​
End Sub
...................................................


I was expecting that after StartActivity("LogIn") the Main activity would pause and wait until LogIn activity finishes. Instead it continues and the log message "In ShowLoginScreen: LogIn.blnLoginClicked = False" appears in the log window even before the login window is displayed.
 
Last edited:

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
You are using the second activity as a modal, (may be you should use a modal dialog for that) but it does not work like that, effectively some code will be paused (animations for example) but and this is a teory: between the startactivity and the activity_create some time elapses and between that time your code within showloginscreen will run.

even if that teory of mine is false, when you go back to the main activity, this one will go to the activity_Resume and not to showloginscreen.
 
Upvote 0

Guardian17

Active Member
Licensed User
Longtime User
Last edited:
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Many thanks for clarifying that! because activity create (of the second activity) runs on the main thread it is now obvious that it holds for the sub where it was called to end.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Many thanks for clarifying that! because activity create (of the second activity) runs on the main thread it is now obvious that it holds for the sub where it was called to end.
It's easy enough to fix, just remove the rest of the code after the call to StartActivity("LogIn") and move it into a different sub routine still in the Main Activity. Then on clicking the LogIn button you can call the new sub with CallSubDelayed and continue.
 
Upvote 0
Top