3

UPDATE: Please see "accepted" solution below
What I have observed is one way is Intent. There is no other proper recommended way.

For me serialization is required when you want to transfer data over network/ or when we need to retrieve objects after a while. Only used in some specific scenarios. But here what I saw is to use intent together with serialization to simply share/pass some data.

According to spec, intent will act as the glue between activities. I would also assume that we can pass instructions /small amount of data to next activity.

My question more specifically is about passing data/big data using intents. Considering that serialization is required when using intents. Is this a good way?

Note: Please consider that ,won't be able to use Parcelable in this specific scenario, since developing a framework independent of android.

Jeril Kuruvila
  • 17,190
  • 1
  • 20
  • 23
  • use Intent.putExtra() and send the object through it form 1 activity to another – cpt. Sparrow Oct 24 '16 at 12:49
  • 1
    Possible duplicate of [How to send an object from one Android Activity to another using Intents?](http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – Anggrayudi H Oct 24 '16 at 12:51
  • 2
    I would really recommend `Parcelable`, for the speed boost if nothing else. If that really isn't possible then yes, serializing your data and sending it through `Intents` is the most common way of giving data to another `Activity`. If that data is likely to be used often, you can also store it in a `SharedPreferences`. – Knossos Oct 24 '16 at 12:51
  • If the performance is an issue here (as you mentioned "big data") here you should see the following tutorial from android developers: https://www.youtube.com/watch?v=IwxIIUypnTE short: the most recommended way is the flat buffers: https://google.github.io/flatbuffers/ Otherwise - just use Gson – mhenryk Oct 24 '16 at 12:52
  • Bundle objects as Parcelables or Serializable and send them as Intents. Parcelable is the faster approach since it occurs at compile time while serialization happens at runtime. Explain the scenario and maybe we can shed light on whether Parcelable should be used. – ichthyocentaurs Oct 24 '16 at 12:54
  • 1
    @Ichthyocentaurs Never do that, there is a limit for Intent extra size, this will unpredictably crash the app once the data grows. – Kelevandos Oct 24 '16 at 13:07
  • @kelevandos not doing this now for sure unless I am absolutely sure about the size of data. But use of non-relational databases like mongo have started increasing data sizes, since web-service guys just dump data for client to process. Nice insight – ichthyocentaurs Oct 24 '16 at 13:15
  • I have shared another approach which is a good way to access datasets. We use an elaborated version of this architecture for managing data caches in various app components. – ichthyocentaurs Oct 24 '16 at 13:20
  • 1
    First two comments are posted without even looking at the description. Others comments are "Are the discussion I was looking for " . Thanks – Jeril Kuruvila Oct 24 '16 at 14:12

3 Answers3

3

Intents should only pass small packets of data. If you need to pass something big, save it to storage or a database, pass an uri through the Intent and then read the data in the receiving Activity.

Passing big data in the intent will cause drastic problems, up to the point of killing your app process (which is very annoying to the user).

There is a process-scope limit of 1MB of data being passed between components. Please keep in mind that this does not mean that you can pass 1MB of data safely, as there may be multiple Intents being processed at a time.

You could also consider using an event bus library, like greenrobot EventBus, but these require a big amount of discipline, as they basically let you pass everything everywhere.

Kelevandos
  • 7,024
  • 2
  • 29
  • 46
1

Intents' extras are indeed the common way. It depends on your data type - primitive types do not require any special work on your size, and custom models should be bundled in a Parcelable object (can't think of why it can't be an option). If you're caching some large data (for example, large pictures), you should consider storing them temporarily on the SD card (as files or in a local SQLite), but this is still your way to go. Try to avoid extra network use and don't cache this data on a remote server.

Another method, especially good for communicating with other types of contexts (services, broadcast receivers) is EventBus.

Neria Nachum
  • 1,519
  • 1
  • 20
  • 37
1

An approach that I have used is to use a Singleton Holder Class for the Data Object. And access it between components of your process. pseudo code is here below. May have compilation errors. Also mind that you would need a purging mechanism and add a way to keep the data fresh.

class DataSet{
    DataSet(String data){
        this.data = data;
    }
    public String data;
}

class Holder{
    private Holder(){
    }
    private static Holder holder = null;
    DataSet object = null;

    private Holder  static getInstance(){
        if(null == holder){
            holder = new Holder();
        }
        return holder
    }

    public void setData(DataSet arg){
        object = arg;
    }

    public DataSet getData(){
        return object;
    }

   }


class Activity A implements View.OnClickListener{
    public void onClick(){
        Holder.getInstance().setData(new DataSet("this is a big object"));
// At this point the data has been set and has process scope.
            startActivity(new Intent(A.this, B.class))
        }

}

class Activity B{
    DataSet data;
    public void onCreate(){
        data = Holder.getInstance().getData(); // This point the data is accessible to Class B
    }
}
ichthyocentaurs
  • 2,173
  • 21
  • 35
  • 1
    Google would probably be angry with you, as this uses Java over Android architecture xD But yeah, a nice idea :-) Just mind that it will only work inside the same process, as different processes have separate VMs in Android ^^ – Kelevandos Oct 24 '16 at 13:30
  • Sometimes java has simple solutions to complicated problems. but yes the intention is to always access this data within the same process. To use this across process just add an aidl layer to this solution. – ichthyocentaurs Oct 24 '16 at 13:32
  • It would not work with aidl, as you would still need to put the data in a Binder, which would crash the sender process :-( – Kelevandos Oct 24 '16 at 13:33
  • because of the size limit? – ichthyocentaurs Oct 24 '16 at 13:34
  • 1
    Yeah - a Binder buffer can hold 1MB at a time. Between processes you would probably get a whole buffer for yourself, so it is better because you can use the whole 1MB predictably, but still, a bit risky. – Kelevandos Oct 24 '16 at 13:36