Hello all, I have an issue that is somewhat related to a previous post of mine here: https://developer.zebra.com/thread/32976
While, based on the answer of that thread, there were some known issues with the older EMDK service and updating to the latest did help a lot; my implementation of the scanner just does not seem right. It is working and with the EMDK update, working a lot better but I wanted to ask those more experienced to weigh in on a possible more efficient approach.
Basically, my application has two main activities. I need to be able to scan on both actvities. What I have done so far is taken the scanner code, very similar to the EMDK docs and the sample application and wrapped it all in a singleton class. The idea is that activity A can get the instance to my singleton scanner class (and since this will typically be first lauch, the singleton can instantiate itself). From activity A, a user can switch to activity B. Activity A is not finished but rather, backgrounded as the two activities can switch back and forth. I know one of the biggest concerns with a singleton is thread safety. Let me say that I don't have to worry about that here but by all means, if there is a better way feel free to give your suggestion.
So, here are the questions or issues I am having so far:
1. If I just get the instance to the singleton with each onResume of the activity (which I tried first), do I have to pass in or update the context each time? GetEMDKManager call requires me to pass in a context. If it is called and instantiated from activity A, the instance will continue to have activity A context. Doesn't this present a problem when I am trying to use it in activity B? Should I update the context when I get instance?
2. I assume you cannot simply update the context because the EMDKManager was created with the other context. Therefore, you would need to completely dispose everything when switching activities so you can call GetEMDKManager again and pass in the new context. Is that correct? This is how I am doing it now. More on that below.
3. I was having issues where, if I just call to pause the scanner during the onPause of activity A, then when you switch to activity B, you get the singleton and call to resume the scanner (which is basically InitScanner) during the onResume, it acted like I had two instances of the class even though it is a singleton because I was getting double scan result event handler raising on activity B.
All that said, I am basically doing this now:
1. Activity A launches and gets the instance of the singleton, it is null so it instantiates itself. It does this in onResume
2. In onPause of Activity A, I call to pause the scanner, then destroy and then dispose the singleton instance.
3. In onResume of Activity B, I call to get the instance of the singleton and again, it is null since I disposed everything during onPause of activity A. This allows me to pass in the new context.
4. In onPause of Activity B, I do the same thing as Activity A so that the scanner is paused and the singleton instance is disposed so that activity A can get the instance again but pass in context when switching back.
Maybe this is the best way but it just feels clunky. Seems that passing the scanner back and forth between activities should be easier. Maybe I am missing something.