Passing a complex object through an intent in Android - android

I have an object which has the following class fields:
int, int, String, MyDatabaseType (custom object), List < MyDatabaseDetail > (array list of custom objects)
Is it possible for me to pass this through an intent/bundle?
I've played around a little bit with serializable and parcelable, but I couldn't get it working. Would I need to make all of custom object types parcelable, instead of just the main one that I want to pass?
Surely there is a better way?

When you pass data in an intent, it must be Parcelable. That is because the intent may passed to a different application, and thus a different VM. As your object may be crossing process boundaries, it needs a mechanism that will allow it to be saved/restored. This is analogous to passing data via a web service call (in this case, the object is "flattened" to something like either XML or JSON).
Even if the intent stays within your application, parcelability allows the object to survive even if Android chooses to kill/re-launch your application, which can happen if memory is running low.
Serialization can be used instead of parcelization, however parcelization is more efficient.

You can make your class Parcelable if it contains variables that are not serializable. If all variables inside your class are primitive or serializable or you can make them serializable, it's easy enough to do it.
You can follow this guide: http://developer.android.com/reference/java/io/Serializable.html
In most cases, all you have to do is to make your class implement Serializable interface and add the following line in your class:
private static final long serialVersionUID = 0L;
(read more in the guide above).

Related

Implementing both Serializable and Parcelable interfaces from an object in Android - conflict

I have an object that i must save to file for reuse. The class of this object already implements Parcelable for use in intents. My knowledge of saving an object to file says to implement Serializable, but when i do, i get an error in the class that contains this object at the putExtra method of an intent because both Serializable and Parcelable have this method.
Is there a way to avoid this, or just a way that i can save my object state and reload it easily?
I have looked at a few articles and i feel no more informed about how i should be saving my object.
Thanks in advance
I believe that Parcelable and Serializable both reaches the same goal in different ways and with different performances. Given that, if some class in your object hierarchy alread implements the Parcelable interface, you can override its writeToParcel method, call the super for it (so the members of the super classes will be written to the parcel if they were implement that way) and then, you should write your attributes to the parcel, always keeping in mind that the order you use to save them is the order you will use to retrieve them latter (FILO data structure)
EDIT
Just cast your object where it complains and tells about the conflict to the class you want to use as described here: https://stackoverflow.com/a/13880819/2068693
I don't know that you can implement both Serializable and Parcelable together but for convert a class from Serializable to Parcelable you can use this plugin:
Android Parcelable Code generator.
First remove implement Serializable then with ALT + Insert and click on Parcelable you can generate your class.
You have options other than Serializable, but that may meet other requirements such as avoiding library dependencies. You can write objects to file using JSON or XML, which has the advantage of being readable. You may also need to consider versioning - what happens when you have files that need to be read by a class that contains a new field. Persistence brings with it some issues you probably don't have passing Bundles/Intents back and forth.
If you choose Serializable I'd recommend structuring your objects so they can be written to and read from a Bundle. Using a static MyObject.make(Bundle) method and an instance Bundle save() method keeps all the constants and read/write in a single location.

How to pass not basic data to activity on creation?

In every example I saw, the data is somehow synonymous to basic (raw) data -- ints, chars, array of bools, and so on -- this is too limiting for me, because I would like to pass a regular object.
So how to pass any data to activity, like for example, instance of MyClass?
I checked the Intent.putExtra -- all I found was basic types + Bundle, but Bundle itself also handles only basic types.
There are several way to do it as described in android guide faq.
I think that in your case static variables could help most.
You could also implement Application and use it to share your data between Activities.
Here is short tutorial on that.
In every example I saw, the data is somehow synonymous to POJO data -- this is too limiting for me, because I would like to pass a regular object (not int, or string, or array of bools).
POJO = Plain Ol' Java Object = "regular object (not int, or string, or array of bools)".
So how to pass any data to activity, like for example, instance of MyClass?
Make it Parcelable.

How to preserve Object data without passing between activities within Android?

Actually,my application flow is like this Home->A->B->Info(form data)->D->Final page.From final page if I press on one button it again navigates back to A page and start the flow from onwards.If I comes to info page I should display the earliear data.Right now my approach is passing parcelable object within all acitivities from A->B->Info->D->Final.If suppose want to use Preferences, doesn't supports the parcelable object and don't want to put each string of object individually within preferences becaus I had more than 10 items within object.Is there any better approach without passing bundle between actvities.
BR,
Developer.
you can create Global class and declare Static variables and use them in anyware in the application.
Example:
public class global_variable {
public static String sample ;
}
where you want to use ;
global_variable.sample = "your value";
You could use any number of technologies to parse your data object into a string and reassemble again. Then you could store the string in preferences.
Take a look at gson to convert objects to json http://code.google.com/p/google-gson/
Or you could google xstream to convert to xml
If you create a class representing your 'object' with appropriate setters/getters and let that class implement Parceable and then pass that class between Activites as a Parceable in a Bundle, would that be bad?
If that would be bad (e.g. if the amount of object data is very big or they are somehow not Parceable in principle) and you only have one meaningful instance of a class at a time you can make that class a singleton or keep it within your Application object.

What needs to be explicity bundled

When bundling an object for later retrieval do I have to bundle objects within those objects?
For example, if I have an object that represents a player in a card game and within that I instantiate an object that represents the player's hand, do I have save the inner object to the bundle or is that automatically included with the outer one?
You cannot bundle any old Object, it has to be a String or a primitive such as boolean, integer, 'byte' or an array of these simple things. In this case yes, the contents of a String[] array are saved with the Bundle.
For more complex structures you can use implement the Parcelable in your object class, but it will be up to you to make sure the object saves all necessary information to it's Parcel and restores it.
java.ui.Serializable is something worth checking. It pretty much automates bundling class and its member variables as long as your class and all required members implement Serializable interface too.
http://www.tutorialspoint.com/java/java_serialization.htm

how to communicate between Activity(s) using non-primitive object

Is it possible for Activity(s) to communicate using user defined object?
p.s.
So far as I know, when I want Activity(s) to communicate to each other, I have to use primitive type of objects, such as int, String, boolean,...etc.
We don't use Serializable, Parcelable and static class.
If talkin about extras when caling intents, you can implement Serializable or Parcelable interface in your objects to pass them through.
You can also put that object into own implementation of Application class and access it in Activity or Service class as described in my other answer. But please keep in mind, that sharing state in that manner may be a sign of more general problem in your design.
You have a few options:
1.You could wrap the more complex structure in a class that implements the Parcelable interface, which can be stored in an extra.
2.You could wrap the more complex structure in a class that implements the Serializable interface, which can be stored in an extra
3.You use static data members to pass stuff around, since they are all in the same process
4.You use external storage (file, database, SharedPreferences)
5.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.
Answer copy from here
Intent myintent = new Intent(Info.this, GraphDiag.class).putExtra("<StringName>", value);
startActivity(myintent);
use the above code in parent activity
and in child activity
int s= getIntent().getIntExtra("<StringName>");
in the same u retrive the float,char,String values

Categories

Resources