Bundle array of json/File/user-defined objects in Android - 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?

Related

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

Android, performance of transferring data between activities

Which one of two ways below has better performance for transferring data between activities?
Activity1: putExtra("id" , customerId)
Activity2: Select on the table and fill Customer object
Activity1: putExtra("customer", customer)
Activity2: Customer customer = (Customer)getIntent().getExtras().getSerialaizable("customer");
I mean send a unique item (like id) to the next activity and then select it from data base OR send the whole object to the next activity and cast it?
Obviously first way. You just send the ID of the object and then read it from the database in second Activity. The other way involves serialization/deserialization which costs CPU-cycles. Even using Parcelable will still use significant CPU-cycles.
On the other hand, I doubt that you would notice any performance penalties unless you are doing this for a bazillion objects. Do whatever is more straightforward and easier to understand/maintain.
Serializable is not recommended in android, check parcelable it's faster.
Serialization vs DB depends on object complicity, only profiling can show the picture, but for a one object it's neglectable.
The fastest way would be to store object in memory and share through singleton.

How to pass large json string to next activity

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.

Alternative method to intents for moving long list of variables between classes?

I've recently been making a very data driven application in which I use a lot of arrays. I've tried searching for a solution, but the problem is quite difficult to word concisely so I haven't been able to find an answer. If there's an obvious one I apologize beforehand.
Currently I load a set of 30 arrays from multiple pre-made databases during an initial class, and I use intents to move this set of arrays back and forth between my classes. The problem is, this results in very long extra sequences of code in every single one of my classes. To give an example, after the initial screen I have to enter the code in every class:
Intent intent = new Intent (getApplicationContext(), NextScreen.class);
intent.putExtra("array1", Array1);
// ... 30 more arrays
and then
Bunble b = getIntent().getExtras();
Array1 = b.getStringArray("array1");
// ... 30 more arrays
I was hoping maybe there would be a way to store all the arrays in some resource or class to just reference later.
I suggest your create a Class that holds all your information , then make this class Parcelable so it can move throught activities : Parcelable example .
Instead of using arrays, create a class with static vector or arraylist.
create setter and getter methods which will update your arrays.
and directly call these methods from multiple classes,you don't need to pass values. just store them in one class, and use the same class, to read write from multiple places
Try using a singleton class. This will be like a "Model" class in MVC patter.
However, exercise caution when using this, as per this discussion:
Singletons vs. Application Context in Android?

Android: Save array to app data

Is it possible to save an entire array (or even ArrayList) to the android app data?
As far as I know you can only do stuff like putInt, putBoolean or putString....
But what about more complex data-types?
Is there a way to do that?
Or do I have to convert the whole array to a String first, and then parse it to an array again on load?
for complex data-types use
Bundle bundle = new Bundle();
bundle.putSerializable("key", object);
intent.putExtra("bundle", bundle);
EDit: or for Arrays use
putStringArray(key,value);
I think yuou dont know there is also functions like putFloatArray(key,Value),putStringArray(key,value).You can use those.
If by "app data" you mean SharedPreferences, then no, you can only save simple types.
However you can save application state via Activities' onSaveInstanceState / onRestoreInstanceState as described here: Saving Android Activity state using Save Instance State
Note that this saves state per Activity, so it's best to have a main activity where you save app state.
You can serialize almost any form of data and save it as a byte stream byte[]
After serialization, store with:
putByteArray("key", serializedData);
You need to implement Parcelable. You should have a look at this tutorial Passing ArrayList across activities

Categories

Resources