Android Question How to see if back button pressed, or activity destroyed, or ...

Russ

Member
Licensed User
Longtime User
Hello,

I have a process like this

"main" activity -> press button -> "loop" activity -> load zxing barcode scanner -> loop activity etc

The loop activity automatically calls the zxing barcode scanner, the scanner returns to it, it calls it again, and repeat.

When I get back to the loop activity it can be for two reasons
- successful barcode scan from scanner activity returns to previous activity
- I was in the scanner activity and pressed the back button

Both of these fire Activity_Resume. I would like to know if I got there because of a barcode scan or because I pressed the back button. Maybe there is a way to test for this or some arguments I can get in Activity_Resume. I believe pressing the back button will destroy the zxing barcode scanner activity and maybe I can test for that?

I hope that makes sense, I'm sure it's fairly basic but I can't find what to search for

Thank you
 

eps

Expert
Licensed User
Longtime User
You need something along the lines of...

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
     
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        Return True
    End If

End Sub

This will stop the App exiting, but of course you may well not want to always stay within the App, so you may well need some other flags or something else in place to allow you to control this. If the Return True isn't set, then it's False and the Event will exit your App.

More info here : http://www.b4x.com/android/forum/threads/activity_keypress.6730/#post-39088
 
Upvote 0

Russ

Member
Licensed User
Longtime User
That's good to know, thank you.

I think that's right but I'd have to implement that in the barcode activity as that's where the back button will be pressed. I don't think I can do that without editing the jar file, that maybe my only option
 
Upvote 0

Russ

Member
Licensed User
Longtime User
The Application_Resume is where I think I'll have to perform whatever I want to do (return to main activity if back button pressed / not come from scan), but that seems to fire prior to the barcode is scanned event. I don't know if I can test for that?

Thank you
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…