1

I have few activities that sets parameters for the last activity For e.g. Let's say I have 4 activities A->B->C->D. In A, B, C activities users selects few parameters that are finally needs to be available to D. Now there are two ways to achieve this

  1. Pass the parameter values through activities using intent.putExtra I.e. A passes its parameters to B and then B passes it to C and so on.
  2. Store these in some static class, Activity D can access it from there.

Just want to know, which of these approaches is best and why?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
CommonMan
  • 3,868
  • 2
  • 24
  • 35

3 Answers3

0

That really depends on your needs and how much information. If there is very little information (maybe 2 or 3 pieces of data) I would say using putExtra() is fine and maybe suggest putting them in a Bundle so you don't have to worry about each extra every Activity.

If you are dealing with much more than that then I would suggest adding them to a separate class. IMO, it will be much easier to maintain this way, less chances for syntax errors (such as misspelling a "key" parameter) and more flexible if you decide to use the data in other ways.

In my current app, it started out using extras, just a few, then grew into many more. It got unmanageable as the data ended up being passed around and used in so many different classes. I just finished up creating a couple separate classes using static HashMaps, ArrayLists, and ContenValues, among other objects. I then had to migrate all of the extras data to these classes.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

Android dev site offers a comprehensive review of cross Activity data passing options and the recommended time to use them, it's best if you review them and chose the solution most suitable for your situation.

Kai
  • 15,284
  • 6
  • 51
  • 82
0

There is also a 3rd method to store your parameters in Shared Preferences

here is the tutorial, i.e how to implement shared prefrences.

If you are using the shared pref in your code then there is no any need of passing your data(parameters) from Activity A->B->C and to D in. you just have to save your data (parameter) once and can regain back on any activity.

Hope this helps

Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124