0

I'm designing an app using NFC on Android.When a tag is read, the system sends to an activity a Tag object. The Tag object is a Parcelable object. Since I want to write later on this tag according to user settings, I need to "cache" this object in some way. I though to use even a sticky broadcast to retrieve it later but starting from Lollipop, the sticky broadcast is deprecated. Another alternative is to save on disk, but as docs says, it's not good thing to save a Parcelable. I guess the problem can happen if I cache on a file the object, the user change android version, the tag object is different and then a crash can happen. I just need a memory cache, but it seems to me there is no way to force Android to keep a memory cache, I could use a sticky service, but Android can kill my service at any time. What is the best way in this case?

greywolf82
  • 21,813
  • 18
  • 54
  • 108
  • do you really need such a STRONG LASTING FOREVER PERSISTENCE for you tag object? :) – agamov Apr 05 '15 at 09:58
  • "Since I want to write later on this tag" -- outside of mannequins or tied-up hostages, most users will move. The NFC tag itself will be taken out of range of the Android device. Even if the user moves the tag back into range later on, the result will be a different instance of `Tag`. Hence, a `Tag` is only ever going to be useful for a second or so. – CommonsWare Apr 05 '15 at 10:28
  • @CommonsWare Yep, but I don't talking about some hours. Even 5 seconds is a problem because I can't pass the Tag object "around". As I said I could use a service but I haven't got any grant that service stays on and I could lost the tag object. – greywolf82 Apr 05 '15 at 14:59
  • @agamov I don't need forever persistence at all. I would need a memory cache just for few seconds but Android doesn't provide any tool to keep a memory cache, a service for example could be destroyed at any time. – greywolf82 Apr 05 '15 at 15:01

1 Answers1

2

When a tag is read, the system sends to an activity a Tag object.

Correct. And, at this point, that activity, and your process, is in the foreground. We will come back to this point later.

Since I want to write later on this tag

There is no "later" of significance. Every millisecond that you wait is a millisecond that the user has the opportunity to move the device out of range of the tag, at which point the Tag object is useless. Even if the user later moves the device back into range of the tag, you will get a fresh Tag object.

according to user settings, I need to "cache" this object in some way.

Not really. You just need to pass the Tag to whatever needs it. It is a Java object, and so it can be passed as a parameter to methods you need. It is Parcelable, and so you are not even limited by Intent barriers, as you can put the Tag in an Intent extra.

Even 5 seconds is a problem

Yes, insofar as the user has probably moved their device away from the tag by then.

because I can't pass the Tag object "around".

Yes, you can, as it is a Java object -- see earlier in this answer.

I would need a memory cache just for few seconds

For five seconds, a static data member will work just fine.

but Android doesn't provide any tool to keep a memory cache

Static data members in Android work just like static data members in ordinary Java.

My guess is that you are concerned about your process being terminated. As noted at the top of this answer, at the point in time when you get the Tag, your process has foreground importance. It will not be terminated until the user does something (e.g., press HOME) to move your app to the background. There is a chance that your background process will be terminated within five seconds. However, if the user decided to leave your app, this would be a signal that the user does not want you messing with the NFC tag anymore.

You are welcome to use a service to try to increase the odds that your process will survive five seconds. At this point, it is far more likely that the user will have removed their device from the tag in those five seconds than your background process will have been terminated in those same five seconds.

Personally, if I were trying to write an app that used NFC and would be reliable, I would write to the Tag immediately upon receipt. Delays mean that may app's reliability will fall, and far more due to user action than due to a process being terminated.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Your reply is formally correct. Since I write a plugin for another app, I can't rely on intent extra or at least I can use a service as "temporary storage" because I need a two step process, it's a long story :). However I agree with you, it's an hack, the NFC android management tells to you: use a foreground activity to write! :) thanks for response. – greywolf82 Apr 05 '15 at 15:28
  • @greywolf82: "Since I write a plugin for another app" -- ick. In that case, yeah, I guess the static data member approach is as good as you're going to get. But, again, I'd worry about the user leaving the tag more than I would worry about the process going away. Users get bored easily. :-) My apologies for getting a bit testy in my answer, but the plugin scenario is fairly uncommon, and it had not crossed my mind. – CommonsWare Apr 05 '15 at 15:33