5

Just wondering what is a better practice to pass information between activites, adding it to a bundle or using a singleton class to store and access this data. I have used both in the past for various android side projects, but I am now working on an android project that is of much larger scale, so would prefer to do things right towards the beginning.

My application authenticates users and then will have to do various queries based on it's id. To minimize coupling between activities, I would think just adding the id to the bundle, and then letting each activity query for the information that it needs, would be the best bet; however to increase responsiveness, I was leaning towards using a singleton class to store persistent information, preventing more queries than need be.

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Brent Hronik
  • 2,357
  • 1
  • 27
  • 43

5 Answers5

8

Personally, I would create an extension of Application to store the state of your app and share data between the different activities. The Application acts as the context for your whole app and Android guarantees there will always only be one instance across your app. Hence it works similar to defining your own Singleton, but using Application will allow Android to take control of the life cycle of your shared data and basically do the memory management for you.

Here are some more details. If you go down this path, you can simply add any getter/setter (or other) method to your application extension to store/retrieve data and do operations on it. Especially the latter can become quite a pain to manage (and keep consistent) when using Bundles passed back and forth between activities. If would only use a Bundle if the data is needed in just one or two places that are neighbours in the activity flow and does not need any (complex) operations to be run on it.

Community
  • 1
  • 1
MH.
  • 45,303
  • 10
  • 103
  • 116
  • Thanks MH, definitely seems like the route to go. – Brent Hronik Sep 23 '12 at 16:25
  • Sorry I didn't mark this response as the correct answer sooner! – Brent Hronik Jan 07 '13 at 18:17
  • 1
    Any suggestions on restoring the state of this persistent information when the application is killed/restarted? There are no callbacks to save this data, so the only thing I could think of was writing it to a SharedPreference. – Brent Hronik Jan 07 '13 at 18:20
  • 1
    @user1456948: `SharedPreferences` would do fine for primitives, but are usually quite limited for more complex data (unless you flatten it to e.g. json). Android offers [different options to persist data](http://developer.android.com/guide/topics/data/data-storage.html), all of which you should probably not use on the UI thread directly. What I normally attempt to do is use my `Application` extension as glorified 'cache' for data that's persisted e.g. on disk or in databases. – MH. Jan 08 '13 at 05:47
  • @MH. I see that you suggest creating a singletons over passing over bundle but is there a use case to prefer using bundle over singleton? – Archie G. Quiñones Jan 08 '19 at 09:00
  • @ArchieG.Quiñones: Honestly, it's been 6,5 years since I wrote that answer. Times have changed, best practises have and so has my opinion. Single-activity apps are a thing, and greatly simplifies the communication flow between the various platform components. And where they aren't, there's view models and friends to provide much better alternatives. – MH. Jan 08 '19 at 18:30
  • @MH. but what if i do not use a single activity? – Archie G. Quiñones Jan 09 '19 at 01:00
2

The better way to go for you is to use SharedPreferences to keep userId you need to keep and reuse. Of course you can use singleton approach or even Application class, but the data will be lost after application is killed.

Eugene
  • 59,186
  • 91
  • 226
  • 333
1

The only time I pass data between Activities via bunlde is if it's something that I won't need to access for a while(i.e the the resID of a resource I want to use only once in the calling activity, etc). I would also think the difference in responsiveness would be very minimal, so that shouldn't be of concern. I suggest the singleton approach

Jade Byfield
  • 4,668
  • 5
  • 30
  • 41
0

Passing bundles is a tedious job. You'll have to pass a bundle for every change in activity to make sure that the value is not lost, even if you're not using the value in the called activity.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
0

Singleton pattern have some bad results. For example:From Main activity you call secondary activity. Phone call interrupted your work.After ending phone call Android is trying to bring secondary activity to screen. Here is my nightmare - many users complaint about exceptions - Google reported to me NULL pointers in my singleton. So you have to provide not only singleton, but all data inside singleton have to be as singleton too. This maked come very complicated :(

Kostadin
  • 2,499
  • 5
  • 34
  • 58