How to pass large json string to next activity - android

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.

Related

passing huge string in intent.putExtra() shows memory error

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

Parcelable out of memory

Suppose we have a list of complex objects (primitives and other objects inside) requested from a server to show them inside a RecycleView. In adapter we need only some data from each object, let's say 70%.
I get from server list with full data objects, and each object implements Parcelable so when I select a item I pass object via intent to MyDetailsActivity like so:
Intent intent = new Intent(context, MyDetailsActivity.class);
intent.putExtra("foo", myComplexObject);
startActivity(intent);
This is working as expected but on some low memory devices I get out of memory errors. So my question is how to achieve this type of data flow?
One possible solution is to implement get/set for MyObj in Applicattion class, and pass it like so but I'n not sure if it's stable.
And of course I can pass just an id from MyObject and do another request call inside DetailsActivity's onCreate(), but this requires user to wait some more seconds to load data.
Any advices or references are apreciated
As you have experienced, sending data through the bundle/intent extras has its memory limits.
Your options are to either keep those objects in memory and access them via some static/singleton instance or to do the data query from your server in the desired activity that will show them in your list.
Another viable options is to store the data in a database for ex and read it once its required but this ofcourse depends on your current architecture... you can checkout Realm, Room, GreenDao database options etc...
Second approach is more suitable for mobile applications since you only load data in list via API which is visible inside the recycler view, you can pass that data to activity and then in activity load the remaining data from another call for that specific item.
I'm assuming that each object of the data coming from the server is not of same size, leading to out of memory on some but not all the objects. So the better approach is to also modify server apt (if possible) to send only info which is required for the list in your call's response and have separate resource url to access full information for that object.
This may result in a minimal delay but it will not cause unexpected out of memory errors which is a big let down for the end user.
You can actually use Application class for this purpose, to make it even better
Set the data in Application class when you intend to pass
Retrieve the data at destination into Local variable from Application
After retrieving data into local variable, set object in Application to null
Using above approach will make sure your objects memory is released as soon you consumed it without holding it all the time throughout Application Lifecycle

When to make an Object parcelable rather than just sending the primitive variables?

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. :)

The best way get Bitmap from android IntentService to get

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

Bundle array of json/File/user-defined objects in Android

how to bundle array of File objects or array of Json objects or array of user-defined objects in one activity and send it to another and how to access them correctly. ?
Primitive types like string is quite easy with
Intent in = new Intent();
Bundle bundle = new Bundle();
bundle.putString("name", "test");
in.putExtras(bundle);
startActivity(in);
Some reading suggested to use Parcelable , some serializable !! What is the right way and how to do it. ?
If you're using JSON already then it's plain old text and you don't need fancy inter-process mechanisms to serialise it.
Personally I'd favour Parcelable over Serializable any day of the week - unless you're writing to permanent storage - but to be sure I'd need to know a little more about the problem you're trying to solve...
What is activity 2 going to be doing with the data from activity 1? Will activity 2 be okay using it's own copy of the data, or would you expect changes it makes to be visible to activity 1?

Categories

Resources