0

I have read many blogs about how singleton are vulnerable in android.so my question is how to maintain such global objects or list in application.i know shared preference is one way but is there any way to maintain such objects or list efficiently.any help will be more helpful.

Hardik Mehta
  • 867
  • 1
  • 12
  • 20

9 Answers9

1

You can use a file or SQLite database to save data in android app. You can check below links to learn more about saving data in a file or SQLite database:

Saving data to a file is ideal to store long sequences of data that are generally read in order https://developer.android.com/training/basics/data-storage/files.html

Saving data to a database is ideal for repeating or structured data: https://developer.android.com/training/basics/data-storage/databases.html

Sonam
  • 572
  • 4
  • 13
1

use sharedPreferences, Sqlite database to manage your objects, singletons are not very good, but static variables are more hard to maintain and will make testing the cide more tough, you can use Shared preferences to maintain a global state if the data is not very large, if there is large amount of data then use of sqlite is recommended.

Shared preferences are extremely easy to use, if you have problem using sqlite though you can use orm libraries for android
here's a link to one: http://greenrobot.org/greendao/

Ankit Arora
  • 509
  • 2
  • 18
0

If you just want to keep a list as Global until your app is running, then create a new class let's say "Helper" and Initialize a Static List in that class. Now you can access that list anywhere within the app by "Helper.yourStaticListName" and you can also add/remove or get data from the list anywhere within the app. But if you want to keep that list even when app has been closed, then there are two solutions for that. First Create a local database "SQLite file" in your app and add/remove or get data from it.

Check this tutorial: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

Second solution is to convert your data into a JSON and convert that JSON into String and save it in Shared Preferences. And whenever you need it just get the string from Shared Preferences and convert it into JSON and parse to get the data. One last thing when you are talking about parsing a JSON, then "GSON library" is a good thing to work with.

Here is the link: http://guides.codepath.com/android/leveraging-the-gson-library

Hope this answer will help you.

Zohaib Hassan
  • 984
  • 2
  • 7
  • 11
  • @ Zohaib Hassan storing in preferences is good practice or not? – Hardik Mehta Apr 25 '17 at 07:00
  • It depends on your requirements. If you want to use your data within the single device and the need for that data is till your app is installed in that specific device and also if your data is not taking too much space (like a Json in String format takes very low space) then Shared preferences is a good option. But keep one thing in mind the data stored in Shared Preferences is not secure, because other apps can easily access and modify your data without your permission. – Zohaib Hassan Apr 25 '17 at 21:29
0

How about using Android Service?

You can initialize / start it when your application started (and also stop them when your application stopped) and then bind them whenever you need (put and get your object / list).

I believe it will be an efficient way.

Erwin
  • 32
  • 4
  • Services are for background operations and not for storing variables only. – vipin agrahari Apr 28 '17 at 09:00
  • @vipinagrahari Still it can use as a choice for storing objects other than singleton / shared preference. Any suggestion you can give to the question? – Erwin Apr 28 '17 at 09:17
0

From conceptual point having a static variables or service-locators is very similar to having Singletons. Hence, having them as alternatives may not be be correct, if the intention is to avoid the Global state and consequences.

We can change Singleton-classes into instances, which are instantiated only once and injected into the components and methods as needed. We can use a IoC-framework to handle the injection part or do it manually with a factory pattern to construct (we can restrict only one instance creation as well) instances of the classes. This discussion thread gives lot of insights on the problem and various options.

Community
  • 1
  • 1
arunk2
  • 2,246
  • 3
  • 23
  • 35
0

So if I understand your question right, you need to store some global variables all over your application if that's so please take a look at this question

basically you create a class that extends application which would store anything you would like on start of your app and all of them can be accessed trough out the app.

hope this helps.

Community
  • 1
  • 1
Pouya Danesh
  • 1,557
  • 2
  • 19
  • 36
0

If you are trying to create a globally accessible object, the first thing you should ask yourself is: Why? Why do you need a globally accessible object? Most of the time you don't, and you can get away with creating an object with a limited scope which is passed around the app.

There are times when you do want globally accessible resources and using a singleton is just one way to accomplish that. According to the Android Docs your data storage options are:

Shared Preferences

Store private primitive data in key-value pairs.

Internal Storage

Store private data on the device memory.

External Storage

Store public data on the shared external storage.

SQLite Databases

Store structured data in a private database.

Network Connection

Store data on the web with your own network server.

Singletons are great, but the do have their own risks based on how they are implemented. Typically developers use this pattern when you are attempting to share a resource within the application, things like Loggers, Print spoolers, etc. There are multiple ways that you can create Singletons in Java, you can use a Lazy Initialization or Static initialization, each has their own pro/cons. In terms of "vulnerabilities", there are issues with whether or not the singleton is thread-safe, who/what can access it, and so on. This is why it makes sense to try and understand the problem you are trying to solve. Personally, I'm not clear on what exactly you are trying to solve, so I can't really elaborate on how this might help or hurt you. All I can say is that the biggest vulnerability is also it's greatest asset, which is that like most global variables, it can be accessed from anywhere at anytime. There can also be an issue whether or not the singleton is thread-safe.

Personally, I think you need to assess what it is you are trying to solve and the pick the appropriate solution. Maybe using a singleton is the correct solution, maybe it isn't. But understanding all your options and the strength/weakness of each one is going to be the best way to solve this issue. Unfortunately, you haven't provided enough context to your problem for me, or anyone for that matter, to give you a solid recommendation.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

The best way to manage global objects is not having them at all. Based on my experience, in a lot of cases there are alternative options instead using singletons. There is so good explained in this post

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
0

shared preference is good but some time you will feel problem when do some modification make static constant variable in one pojo java class and use this variable anywhere.because shared preference will not change value after use or unless you dint modify .shared preference retrieving and storing is not very unfriendly. if you use constant you can modify easily .only one class you have to hit

android_jain
  • 788
  • 8
  • 19