3

Most the app so far I had created, I fetch the data from Network and store it in a singleton class to share data between Activities, when I'm done with those data, I usually clear those setting it to NULL, its work perfectly well

My Question is which approach is better ??

  1. Use parcelable
  2. Writing in database and Query for it
  3. Singleton classes

When exactly we need to use Loaders ?? Why we can't share the data through Loaders ??

If question is repeated ... Please ignore ??

Sufian
  • 6,405
  • 16
  • 66
  • 120
sreekumar
  • 2,439
  • 1
  • 21
  • 27

4 Answers4

4

Answer to first part

  1. Parcelable:

    The best way to pass data between Activitys or Fragments is by using Parcelable objects. It is said to be more optimised than Serializable. There are couple of libraries/plugins which can help you create Parcelable objects. Lately I was referred to Parceler, created by John Carl. However, I personally use Android Parcelable code generator by Michal Charmas, plugin for IntelliJ IDEA and Android Studio.

  2. DataBase or SharedPreferences:

    Using DataBase or SharedPreferences to pass data between Activitys or Fragments sounds weird, and since they are not designed to be used this way, it will only create a mess.

  3. Singletons:

    Read this very informative post Singletons are Pathological Liars.

Conclusion:

I recommend Parcelable or if you want to be real lazy then go for Serializable (it's not terrible but it's not great either, according to most).

Don't mess up your code by using singletons, DataBases, static fields, etc. They will come back and haunt you.

Answer to second part:

When exactly we need to use Loaders

Loaders, which will be AsyncTaskLoader when you use, are basically used for situations where we want to retrieve data from the server (via web API), or doing something in background. It is similar to using Thread or AsyncTask but is very powerful as it isn't destroyed on screen rotation, unlike those two.

You should read How to Use Loaders in Android and Alex Lockwood's posts on Loaders (this is a series of 4 posts. Very detailed and great).

Community
  • 1
  • 1
Sufian
  • 6,405
  • 16
  • 66
  • 120
  • @sreekumar I just added a little more info about Loaders (the last paragraph). I hope you find it useful. – Sufian Oct 27 '15 at 07:52
0

It all depends on the way you want to use the data.If you want to use the data in future, as in after the application is killed and re launched you should save it in a database. I would prefer parcelable over a singleton as I don't have to bother about clear the data. According to the Documentation we generally use loaders to load data asynchronously and to monitor a the data source for change. To my understanding you aren't doing either of them, hence loaders are not required in this case.

Umang
  • 966
  • 2
  • 7
  • 17
0

1.Database: If you are going to use the network data in future or you are going to do some query operation to perform filtration according to requirement,it is preferable to go with db.

2.Singleton Class: Most of the developer use this because it is more efficient,the values can be changed and retrieve easily with the help of getters and setters.

Asif Sb
  • 785
  • 9
  • 38
0

Here is a very cool way of passing data to another activity I read this somewhere else on Stackoverflow and always use it now. It may not fit your use-case but it sounds like it will.

For example, say you want to pass a parcelable "DataModel" from ActivityA to ActivityB.

Inside ActivityB create a public static method called 'start' like this.

private static final String DATAMODEL_KEY = "datamodel_key";

public static void start(Context context, DataModel dataModel) {
        Intent intent = new Intent(context, ActivityB.class);
        intent.putExtra(DATAMODEL_KEY, dataModel);
        context.startActivity(intent);
    }

To start ActivityB simply call the 'start' method above like this

ActivityB.start(this, datamodel);

Use 'this' if called from an activity, or 'getActivity()' from with a fragment.

This will contain the code for starting ActivityB inside ActivityB, like the private static final DATAMODEL_KEY field and such. To answer your question though, go with option 1 and use parcelables and try out the code I posted above to help with starting the Activity with the data.

Nick H
  • 8,897
  • 9
  • 41
  • 64