Android Question Activity Reloads on Close

xor83

Member
Licensed User
Longtime User
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean

    Select KeyCode
        Case KeyCodes.KEYCODE_BACK
                                                         
            closeCounter = closeCounter + 1                    ' Handle the user exit request
 
            ' First time so start the counter
 
            If closeCounter = 1 Then                 
                closeTimer = DateTime.Now
                ToastMessageShow("Press Back again to exit", False)
            End If
 
            ' Second time but not within 3 seconds, reset to "First time"
 
            If closeCounter = 2 AND DateTime.Now > closeTimer + 3000 Then
                closeCounter = 1
                closeTimer = DateTime.Now
                ToastMessageShow("Press Back again to exit", False)
            End If
 
            ' Two tries within 3 seconds, we're done
 
            If closeCounter = 2 Then
                closeCounter = 0
                ShoulIClose = True ' This tells others to close
                Activity.Finish
            End If

            Return True
        Case Else
            Return False
    End Select
 
End Sub

I used the above code but my activity reloads and i have to click back again to close the activity, why this is happening?
 

xor83

Member
Licensed User
Longtime User
I have one Main activity and one other activity named "Listing", I call "Listing" activity from main and close the Main activity, the above code is written in "Listing" activity this code close the "Listing" activity and set Main.ShouldICode Variable to true to close Main activity if its still open, But When I call close the "Listing" activity its closes but reloads again and I have to click back button again to close. On 2nd time it works properly but first time it always reloads the "Listing" activity. I just want to know why its reloads to "Listing" activity again?
 
Upvote 0

xor83

Member
Licensed User
Longtime User
Yes I finish the main activity when I call the StartActivity(Listing). But When I close the Listing Activity it reloads again and I have to press back button again and this time it works.
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
What is the context of the ShouldIClose variable? I would think it should be defined in process globals in the main activity and used in Activity_Resume in main to finish. See the attached.

The code you have listed (which I believe I posted) should probably be used in main to exit the application. If you are in an activity (listing) that is started from main, a simple back press to exit listing and then return to main. If it's used in the listing activity, there is no way to return to the main activity as it will then exit.
 

Attachments

  • Test2.zip
    8.3 KB · Views: 178
Upvote 0

xor83

Member
Licensed User
Longtime User
Thanks all for your help and ac9ts. I debugged but strangely it reloads the listing activity, finally I ends-up adding this code to listing activity on Activity_Create

B4X:
If Main.ShouldIClose Then
     Activity.Finish
     Main.ShouldIClose = False ' Avoid closing again if user start app again
End If
 
Upvote 0

xor83

Member
Licensed User
Longtime User
I have one question regarding Activity.Finish , In my second activity I am checking users loggedIn status and if he is not logged in then I close the 2nd activity and do StartActivity(Main) but the execution keeps on going after Activity.Finish, I am expecting execution to stop after Activity.Finish. How can I stop execution after Activity.Finish? Actually this is the reason my activity start 2 times which is the main issue I mentioned in this post.

B4X:
If     CommonCls.IsLoggedIn = False Then
        StartActivity(Main)
        Activity.Finish
End If
 
Upvote 0

xor83

Member
Licensed User
Longtime User
Activity.finish and startactivity are put into a queue which is handle after the SUB ends.
You want to say that I put Activity.finish and startactivity in different sub and call that sub?
Something Like this?

B4X:
Sub CloseMe  
  Activity.Finish
  StartActivity(Main)
end sub
 
Upvote 0

xor83

Member
Licensed User
Longtime User
CallSubDelayed does not worked and the execution moved to below lines. I used "return" keyword that worked, can you share some thoughts on this?

B4X:
Sub Activity_Create(FirstTime As Boolean)

If CommonCls.IsLoggedIn = False Then
StartActivity(Main)
Activity.Finish
return ' This works as I wanted but don't know if this is the correct way to do this.
EndIf

Log("Dont run this 1")
Log("Dont run this 2")
Log("Dont run this 3")
Log("Dont run this 4")
End Sub
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…