Android Question Help Needed, multiple instances of app launched after device rebooted

walterf25

Expert
Licensed User
Longtime User
Hello All, i'm in need of you guys helping me brainstorm in order to figure out this issue, I have an app i've made which uses the USB_HOST (HID) library, i'm communicating with a medical device, everything works fine except that while testing, one test requirement is to cycle power on the Android device, while having the usb unit plugged in.
The Android device reboots just fine, and the application gets launched automatically since the usb cable is plugged in and permission has already been granted.
The app also starts just fine and starts communicating with the medical device, the problem is that, it seems as if the operating system launches the app multiple times one after another, therefore confusing the communication between the android device and the medical device.
I also noticed that one of the timers is being fired up even before enabling it, this is not the first time i've noticed this behaviors with timers which is why i try to avoid them, but in this case i need to have a timer, in case there's a checksum error, i need to re-start the handshaking with the device from the beginning so i have a timer to wait for 5 seconds before re-starting communication again.
My Main problem right now is that the app re-starts multiple instances after the android device has been rebooted, I'm not sure why this is, but if anyone has any idea or has came up this same issue, i will really appreciate your help or thoughts on this. I've tried three different devices so far, and i get the same behavior. If I restart the android device without having medical device plugged in, and wait for the device to fully boot up, and then if i plug the usb device in, everything works just fine the way is supposed to, i'm not sure what the problem could be, maybe an operating system issue? I don't know, hopefully someone here has an answer.

Thanks all
Walter
 

walterf25

Expert
Licensed User
Longtime User
how does the app get started then?
Sorex i'm guessing is a function of the USB library, as long as it detects the USB cable being plugged in the app will always launch automatically, If i exit the app, then unplug the usb cable and then plug it back, the app gets launched automatically.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
so that stuff in the manifest is actually something that makes the OS aware that it should start you app when that device connect?

that's neat if it works like that.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
so that stuff in the manifest is actually something that makes the OS aware that it should start you app when that device connect?

that's neat if it works like that.
Yes, at least that's the way i understand it.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Try logging to a file.
To see how many times Activity_Create and Activity_Resume are being called.

B4X:
Sub vLog(message As String)
    Try 'No reason to crash here!
        DebugLogger(message, "debuglog")
        Log(message)
    Catch
        DebugLogger(LastException.message, "debuglog")
    End Try
End Sub

B4X:
Sub DebugLogger(s As String,F As String)
    If File.ExternalWritable = False Then Return
    Dim TW As TextWriter
    TW.Initialize(File.OpenOutput(File.DirDefaultExternal,F,True))
    Dim Line As String
    Line = DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now) & " -- " & s
    TW.WriteLine(Line)
    TW.Close
End Sub
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
Hey Straker, thanks for your thought, it actually makes sense, however the Toastmessage should only be executed from my app, and not from any other app.

If with B4A open a completely new project and in the Activity_Create you just write
ToastMessageShow("FirstTime=" & FirstTime, True)
then you'll see the ToastMessage EVERY TIME the app is destroyed and then created.

For example if you flip the screen vertical-horizontal your app will be destroyed and re-created, so the Activity_Create event will fire with FirstTime=False (that's because the activity is re-created but using the same process maintaining all the variables declared in Process_Globals - well, I'm not completely sure of my last sentence). You will see the ToastMessage appear once at the real startup of the app (FirstTime=True) and then you'll see the same message (with FirstTime=False) every time you flip the screen.

Therefore probably your app during the bootstrap has been destroyed and recreated (I mean: not ExitApplication which will destroy the whole process) just like when you flip the screen...
See the documentation http://www.b4x.com/android/forum/threads/android-process-and-activities-life-cycle.6487/
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
More, you say you have the serial management in the activity. I don't think it's a good place because the activity can always be destroyed or paused. It's better to develop the communication in a service which is (more or less) always running.
Be careful: I'm a completely newbie in B4A: my suggestions could be wrong - It's better you ask confirmation to an expert...
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Upvote 0

walterf25

Expert
Licensed User
Longtime User
No, now I think it is a bug in Android.
You might want to try that stackoverflow link if you manage to wrap your head around it.
I think you're right, it must be a bug, a really dumb bug, lol. Thanks for your help guys, i will try to find a way around it, i'm looking at the links you posted.

Thanks,
Walter
 
Upvote 0
Top