Reputation: 19960
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.
Upvotes: 3
Views: 962
Reputation: 2175
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
}
}
Upvotes: 1
Reputation: 7082
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.
Upvotes: 3
Reputation: 1529
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.
Upvotes: 1