0

Why I must use putExtra() function to transfer some data between my application activities? One of my coworkers said me this is just annoying to use this function to transfer any information. Why not use static variables to do this transfer through intents?

  • 1
    https://stackoverflow.com/questions/49084296/android-does-passing-values-through-static-variable-cause-securtiy-issues/49084631?noredirect=1#comment85395927_49084631 – CommonsWare Mar 09 '18 at 15:21
  • @CommonsWare It's not my point. It is saying that static variables are more secure and so it is somehow a proof of that we shall use static variables not putExtras() – MohammadReza Ganjkhanlou Mar 09 '18 at 15:31
  • 3
    That is what I wrote in my answer. I linked to a comment, where I addressed why we would ever bother using extras. – CommonsWare Mar 09 '18 at 15:36
  • you can't ignore intent extra data. Maintaining static params in the application with lead to you in the inconsistent state. Plus, you will have a loose code with loosening traceability. – Shubham AgaRwal Mar 09 '18 at 15:56

1 Answers1

2

PutExtra() places all important objects inside of the Intent. Thanks to this when your Activity or Fragment will be rebuild (rotatation or crash), the Intent will last and provide you with correct data.

Using statics in such case can lead to unstable state and NPEs

muminers
  • 1,190
  • 10
  • 19