Search code examples
androidnfcassets

A refernce sheet for android project


I have a NFC tag and I have to read from it a text, and save this text some where to use the parameters in it in many activities and fragments.

this parameters I should be able to delete or overwrite it when a user decide to exit the program or to read another tag.

I didn't like the idea to transfer this parameters over the activities since they are constant in the whole session for example the ID number of the tag and the manufacturer of it.

I thought also to creat a file in Assets and read it every time, but I thought there should be better way to solve this problem.


Solution


  • There are several ways to how you make the values accessible throughout the project.

    • Using Shared Preferences:

    You can use shared preferences, where you can create variables for your fixed values such as TAG_ID and MANUFACTURER_ID. Every time you tap a new card you can update them, or anytime you expect them to be changed.

    • Using a Model Class:

    You can also create a Model Class (May be Singleton as well) which holds the TAG_ID and MANUFACTURER_ID. Initialize the object of this model class every time there is new Tag tapped. You can also access/change them anytime using getters and setters.

    • Using Static variables:

    You can define static variables to store your desired values and you can simply access them wherever and whenever you want to. This will workout only with less numbers of variables. Increased static variables could affect the performance.