Android Question StartActivity doesn't pause current activity

Discussion in 'Android Questions' started by DrAlex, Jul 24, 2015.

  1. DrAlex

    DrAlex Member Licensed 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: Jul 24, 2015
  2. Enrique Gonzalez R

    Enrique Gonzalez R Well-Known Member Licensed 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.
     
  3. Enrique Gonzalez R

    Enrique Gonzalez R Well-Known Member Licensed User

    DrAlex likes this.
  4. Guardian17

    Guardian17 Active Member Licensed User

    Last edited: Jul 24, 2015
  5. Enrique Gonzalez R

    Enrique Gonzalez R Well-Known Member Licensed 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.
     
  6. RandomCoder

    RandomCoder Well-Known Member Licensed User

    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.
     
    lemonisdead likes this.
  7. DrAlex

    DrAlex Member Licensed User

    Thank you all very much for clarifying the issue. I have solved my problem by using the CustomDialog from the Dialogs library.
     
  8. DrAlex

    DrAlex Member Licensed User

    Enrique Gonzalez R likes this.
Loading...