2

I'm developing an app which is supposed to display different content based on information which comes from NFC tag. The basic idea: User scans NFC tag -> Android sends Intent -> my activity receives the intent -> parse NFC data from intent -> fetch content from DB -> display. The problem is that I'm not sure where to put Intent parsing code. So far I've got 2 options:

1) I get NFC Tag object from Intent in my activity -> read data and convert it from byte[] to something more convenient -> parse this data to ViewModel -> VM does the rest. According to Android documentation, the View should be as simple as possible. But in my case Activity seems to be doing too much work as parsing data from intent and converting it. Besides, it's definitely not View related code.

2) I get NFC Tag object from Intent in my activity -> pass Intent object to ViewModel -> ViewModel parses data from Intent and does the rest. In this case, Activity does nothing but just passing data, so it stays pretty simple, however there is an issue with ViewModel, now it needs to know how to work with Intent object and NFC data. According to Android documentation ViewModel shouldn't know anything about Android specific stuff i.e. there should be no android.* imports. If I work with Intent and NFC in VM then I have to import some Android specific classes.

What is the best practice to get data from Intents in order to comply with MVVM?

RKRK
  • 1,284
  • 5
  • 14
  • 18
SergeiTonoian
  • 341
  • 2
  • 13

0 Answers0