Android Question Return to previous app

barx

Well-Known Member
Licensed User
Longtime User
I fear this will be a limit of Android itself, not B4A.

I have created a small app to open the ZXing Barcode scanner app from the notification bar. All was working well, but today I actually put the app into real use and noticed something that I had assumed would work my way.

The idea of the app is to reduce the barcode scanner / code input process from:
Close the app your in > launch barcode scanner > scan barcode > close barcode scanner > re-launch app > paste code.

To (from an open app):
Open notification bar and tap ZXTrigger (barcode scanner loads) > scan barcode (barcode scanner auto closes with code in clipboard) > paste code.

Everything works right up to the end. Once the barcode scanner auto closes, the previous app is no longer in the background. I was hoping it would just pause and once the scanner closes, the previous app would resume. Does everyone follow?

So is there anyway to get the previous app in code. Obviously it can be loaded by long pressing the home button and picking it out, but it would be nice to get rid of this step.

Thanks for looking.

p.s. this is the app if anyone wishes to take a look ;)
https://play.google.com/store/apps/details?id=barxdroid.ZXTrigger
 

qsrtech

Active Member
Licensed User
Longtime User
I'm curious why you don't just use icefairy's built in scanner library. no need to d/l zxing app. seems to work great for me. and I suspect you'd want to try and send some kind of intent to the previous app.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
I'm curious why you don't just use icefairy's built in scanner library. no need to d/l zxing app. seems to work great for me. and I suspect you'd want to try and send some kind of intent to the previous app.

It is not 'my' app I want to launch the barcode scanner from. I want it to be usable for any app. My personal use (and the reason I created this) is data input into a web form in an internet browser.
 
Upvote 0

qsrtech

Active Member
Licensed User
Longtime User
But you're using an your own intermediate app to launch a scanner and grab the barcode so you could still use icefairy's library. It looks like there's an "activityManager" object in android that you should be able to pull the recent activities and maybe send an intent to it. Not sure if this object is exposed in b4a, maybe you can use reflector to access it if it isn't.
Check this thread. I think it has what you're after:
http://www.b4x.com/android/forum/th...ications-and-package-names.29735/#post-172988
 
Last edited:
Upvote 0

treehousefrog

Member
Licensed User
Longtime User
Could you use packagemanager to view running apps and then launch the most recent one? You could put it in Activity_KeyPress and override the back button...
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
But you're using an your own intermediate app to launch a scanner and grab the barcode so you could still use icefairy's library. It looks like there's an "activityManager" object in android that you should be able to pull the recent activities and maybe send an intent to it. Not sure if this object is exposed in b4a, maybe you can use reflector to access it if it isn't.
Check this thread. I think it has what you're after:
http://www.b4x.com/android/forum/th...ications-and-package-names.29735/#post-172988

I understand what you are saying but I don't see the point in re-inventing the wheel. Also, last time I looked at icefairy's implementation, it had issues with a few devices. As for the activityManager, I spotted that late last night too and I think it could be the answer, going to try knock a lib together to gain the required features. Thanks
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
What happens if you do not start the barcode app from your app, and then when your activity is visible you press on the back key? Does the previous app resume?

As it stands Erel, technically, there is no activity in my app, it is just a transparent activity that simply launches a service, the service deals with everything. I will edit the project or create a fresh sample to try it out.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Could you use packagemanager to view running apps and then launch the most recent one? You could put it in Activity_KeyPress and override the back button...

Can't see anything in the package manager that would be of use here, I think the activity Manager will be the way, which as far as I know is not available to b4a. Maybe Soon! ;)
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
As it stands Erel, technically, there is no activity in my app, it is just a transparent activity that simply launches a service, the service deals with everything. I will edit the project or create a fresh sample to try it out.
OK, if I just load an activity in my app and the press back, the previous app (internet browser in this case) is still open. What does this mean?

Does it mean that the Barcode Scanner is closing the previous app or that it is being closed because My app loads which then in turn calls another. i.e. I go too deep? or something else?

Edit: Just remembered, my notification calls the barcode scanner directly, not my app > Barcode Scanner. Maybe it is in the intent. Could the intent FLAG affect this?

thanks
 
Last edited:
Upvote 0

qsrtech

Active Member
Licensed User
Longtime User
Let me know if you figure out how to get the "last activity", I'm hoping I can do the same and grab the html source from a webpage when a user clicks my notification.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Edited previous post.
 
Upvote 0

treehousefrog

Member
Licensed User
Longtime User
This is what I used for a similar effect :-D

B4X:
Dim o As OperatingSystem
    Dim tasks As List = o.getRunningTasks(10)

    Dim task As Object
    Dim whichapp As String
    task = tasks.Get(1)
        Dim r As Reflector
        r.target = task
        Dim bi As Object = r.GetField("baseActivity")
        If bi <> Null Then
            r.target = bi
            whichapp = r.RunMethod("getPackageName")
        End If
  
Try
Dim pm As PackageManager
Dim il As Intent
il = pm.GetApplicationIntent (whichapp)

StartActivity (il)
Catch
ToastMessageShow ("Failed to launch app! " & quick.Get(position) & " Is it installed?", True)
End Try

P.S. 'Get 1' because '0' is your app! ;-)
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Do a test and start your activity instead. Does it still return to the home screen when you press on the back key?

Just done the test, my activity is loaded and the upon pressing back the home screen is indeed displayed. i.e. previous app closed.

in the lib the pendingIntent, either way, is set to

PendingIntent.FLAG_UPDATE_CURRENT
B4X:
PendingIntent.FLAG_UPDATE_CURRENT
 
Upvote 0

qsrtech

Active Member
Licensed User
Longtime User
Since I needed to so something like this as well (but not with a scanner) I decided to whip something together for you. The attached use icefairy's scanner lib. I think you'll see the demo works nicely, at least on my note2 phone. The only issue was figuring out if the user pressed the back key when scanner was open. The only solution I could come up with in the limited time was to use a timer to close the activity and go back to previous app. I suspect the scan lib could be updated to return "" (empty string) if the user clicked the back key. Hope this is what you're after. Good luck
 

Attachments

  • returnapp.zip
    7.2 KB · Views: 137
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Since I needed to so something like this as well (but not with a scanner) I decided to whip something together for you. The attached use icefairy's scanner lib. I think you'll see the demo works nicely, at least on my note2 phone. The only issue was figuring out if the user pressed the back key when scanner was open. The only solution I could come up with in the limited time was to use a timer to close the activity and go back to previous app. I suspect the scan lib could be updated to return "" (empty string) if the user clicked the back key. Hope this is what you're after. Good luck

Interesting example.

unfortunately the integrated version of zxing doesn't give enough flexibility. ie. the scan window is very small and I don't see a way to adjust it. There is no way to have flash on etc. The default ZXing app scan window is much bigger and options for flash, etc. Thanks for your help though ;)
 
Upvote 0

qsrtech

Active Member
Licensed User
Longtime User
Interesting example.

unfortunately the integrated version of zxing doesn't give enough flexibility. ie. the scan window is very small and I don't see a way to adjust it. There is no way to have flash on etc. The default ZXing app scan window is much bigger and options for flash, etc. Thanks for your help though ;)
I know the scan window size issue. I've contacted icefairy and he game me some code to change the size, but I have to update the actual lib. As for the light, have you tried just turning on the light before calling the scan? If that don't work maybe icefairy can shed some light on how to implement it in his library.

Other than that does my sample work as expected on your device? It'd be good to know if it works the same on all devices.
 
Last edited:
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
I know the scan window size issue. I've contacted icefairy and he game me some code to change the size, but I have to update the actual lib. As for the light, have you tried just turning on the light before calling the scan? If that don't work maybe icefairy can shed some light on how to implement it in his library.

Other than that does my sample work as expected on your device? It'd be good to know if it works the same on all devices.

Yes it does, and the interesting part is that is doesn't close the previous app. Yet is I do a similar thing calling Barcode Scanner app, the previous app is still closed. strange AND interesting
 
Upvote 0
Top