I'm developing Web-client on Android. I use IntentService for http-request. As a result, the object are formed by IntentService which have not only the primitive types, but also, for example, Bitmap object field. Tell me, please, best way to pass the object in the Activity, or some another class.
I try used ResultReceiver, but callback method get only Bundle-object:
void onReceiveResult (int resultCode, Bundle resultData)
Bundle-object is only suitable for storing simple types. Translate large Bitmap-object as byte array is not recommended.
The second path, that to pack the object in the Parcel and set in Intent. Then catch it using BroadcastReceiver. How about sending large object in Broadcast message?
Perhaps you can just save the image and pass through the OutputStream way? Or just save it as a static variable somewhere? I'm not an expert of Android, the main thing I want to get fast app. Thanks for your answers!
Bundle-object is only suitable for storing simple types. Translate large Bitmap-object as byte array is not recommended.
Yes, you should never transfer large data via Intent. It's not about data type, it's about size.
The second path, that to pack the object in the Parcel and set in Intent. Then catch it using BroadcastReceiver. How about sending large object in Broadcast message?
It's same thing, sending a message still requires Intent which has limitations.
Or just save it as a static variable somewhere?
This will work but this is a bad architecture. If you do this you will always have to think about that static field and be sure at all times that you are not keeping a static reference to the Bitmap you no longer need. I strongly don't recommend this.
Instead, you can store your Bitmap object to disk and then read it in your Activity. Here is an example: Save bitmap to location
Related
passing huge string in intent.putExtra() shows memory issue and results in crashing of application.
Yes. You cannot put large objects in an Intent like that. If you need to pass large objects you will need to use another method:
Store the object in a static variable that can be directly accessed by the sending and receiving Activity
Store the object in a database
Store the object in a file
After researching for a while there is still something unclear for me:
When is it worth to implement Parcelable rather than just getting the variables and send them to a new Activity?
If i search for example for something like "RecyclerView click open new Activity", almost everyone posts code that extracts the values with getter methods in Activity 1 and sends them via multiple .putExtra calls to Activity 2. Almost no one seems to suggest to implement Parcelable.
I also fail to see where Parcelable saves effort or makes the code more maintainable, since i have to call .getXXX for every value in the target Activity anyways.
So let's say I have a RecyclerView, I want to open a new Activity on Button click and i want to send 3 variables out of 1 Object from 1 ArrayList. Should i send the variables directly over 3 .putExtra calls or implement Parcelable and send the whole Object?
In your case sending the 3 primitive values with putExtra should be enough. Parcelable is to send objects. Normally these objects contain objects which contain other objects or array of objects. In summary it is used to send complex objects. It's also necessary if you need to send an object to your service as part of the body of a request.
Don't use Parcelable if u have less no. Of data elements because serialization itself take some time. But sending them individually will be faster.
Using parcelable for something as trivial as this would be an overkill as it involves the overhead of constructing a new object for only a few values. It would be better if you just add the .putExtra() calls for the values you need to share, that should be enough, as the others before me said. :)
Just like it says on the tin. I have a decent sized object, consisting of about 20 variables, some of which are themselves android objects (Calendar, Uri). An arbitrary number of these objects are stored in a database.
I need to pass an individual object by intent to a broadcast receiver. When I create the intent I already have the entire object as a local variable and would not be querying the database. Is it more efficient to pass the entire object as a parcel, or to pass the database id and pull the object from the db in my onReceive method?
I suspect the former, but until yesterday I was doing the latter because I was too lazy to implement parcelable. I did, but now I'm second guessing myself.
P.S. I'm no CS major. In this case I define most efficient to mean least impact on user experience.
If the object in question is small (under 1MB), then going the Parcelable route should be more efficient. Basically, you avoid the disk I/O (and the accompanying complexity of trying to do that on a background thread when triggered by a broadcast).
I am sending large JSON string where records of JSON array length is 800 but currently when I start that Activity then application quits without any crash message but when I reduced the records to 100 then it works perfectly.
I am doing like below
Intent myIntent = new Intent(getActivity(),
ActivityName.class);
myIntent.putExtra("jsondata", respUserData);
getParentFragment().startActivityForResult(myIntent,
pick_plan);
getActivity().overridePendingTransition(
R.anim.lefttorightanim, R.anim.righttoleftanim);
So what it is correct way to send large JSON to next Activity ?
Thanks in advance.
Don't pass large String or Large Data directly in to intent, you have to use Application Class.
Set variables and methods in application class.
To get more information about Application Class view this answer.
Since it's probably not a global state that you pass, I advise against using the Application Class. If it is data that you use all the time you can easily store it there.
For passing a large amount of data using the provided way with putExtra() should work fine. Apparently the Bundle doesn't handle your JSONObject correctly. You can try to convert it to a String, pass this to the Intent.putExtra() and then reconstruct the JSONObject from the String. This will have some impact on the performance, but should be usable as a workaround.
You can pass values as you mentioned. It will be as a bundle so it can hold large amount of data also.
We can pass Arraylist, model classes etc using the intent.
Suppose you want to start a new activity and pass it some data from the current activity. If the data is of a primitive type you could simply use an intent and add extras, but how would you do this for more complex data structures like arraylists or objects?
You have a few options:
You could wrap the more complex structure in a class that implements the Parcelable interface, which can be stored in an extra
You could wrap the more complex structure in a class that implements the Serializable interface, which can be stored in an extra
You use static data members to pass stuff around, since they are all in the same process
You use external storage (file, database, SharedPreferences)
As the person who just posted noted, use a common component, such as a custom Application or a local Service
What you do not want to do is pass big stuff via extras. For example, if you are creating an application that grabs pictures off the camera, you do not want to pass those in extras -- use a static data member (icky as that sounds). Intents are designed to work cross-process, which means there is some amount of data copying that goes on, which you want to avoid when it is not necessary for big stuff.
One option I am aware of is storing the data you are using in an Application object which all your activities can retrieve from context.
I have also heard of using Google Protocol Buffer to achieve a higher performing solution