What does putExtraData do in Kotlin Intent? - android

I am trying to send an object (via intent ) of data class type and came across this method
putExtraData in Kotlin. ( Could not find a similar method in Java).
What does it do ? Can i send an object of this type using that method? I was wondering because i may avoid having to use Parcelable and Serializable if this is possible.
data class Properties(val actionBarColor : Int){
}

Related

#Parcelize and Superclass, doubling data because of constructor requirement

I have a Result parcelable class that is supposed to serve as a container for a key and a parcelable data. This is how I define it:
#Parcelize
open class Result<out T : Parcelable>(val key: String, val data: T?) : Parcelable
The problem is that when defining the child class, in order for #Parcelize to work, I need to add val to the object in the constructor, essentially making the data being written twice to the parcel twice. Here is an example:
#Parcelize
class LessonFinishedResult(private val lessonActivityData: LessonActivityData) :
Result<LessonActivityData>(LessonActivityData.LESSON_KEY, lessonActivityData), Parcelable
I would like to have this:
#Parcelize
class LessonFinishedResult(lessonActivityData: LessonActivityData) :
Result<LessonActivityData>(LessonActivityData.LESSON_KEY, lessonActivityData), Parcelable
But it is not allowed. Is there a smart way I can get around this? And another thing, is there a way I can avoid having to manually use Result<LessonActivityData> and have the type be inferred automatically from the object being passed?
Thanks!
essentially making the data being written twice to the parcel twice
It isn't; the code generated by #Parcelize doesn't care about the superclass at all. It can even not be Parcelizable. Only the primary constructor parameters (in this case val lessonActivityData) are written/read, and when reading it's enough to pass the correct parameters to the superclass constructor.

How to send MutableList<Point> from intent to another in kotlin

I'm trying to send MutableList<Point> array list as Extra from Intent to another in kotlin.
Declaring the list:
private var thePoints: MutableList<Point> = mutableListOf()
and here is how I add items to it:
if (startStationPoint != null) {
thePoints.add(startStationPoint)
}
And am using this method to send it to the other Activity:
navigationActivity.putParcelableArrayListExtra(
"thePoints",
thePoints)
It gives me this error:
Type mismatch:
Required: ArrayList<out Parcelable>!
Found: MutableListM<Point>
as am using putParcelableArrayListExtra as there is no such thing to put points arraylist extra.
The error explains you are using as argument a mutable list but you have to use an array
thePoints.toList().toTypedArray()
I'm not sure if that will work because Point can not meet the conditions
You could create a class representing the point
data class SerializablePoint(val longitude: Long, val latitude: Long) : Serializable
And then use .map to convert them.
Or you could do the same with parceleable.
Another options is to try Pair which is Kt API, but again I don't know if parcealability or serializability conditions are complied.
To figure it out ctrl+click on any of those classes and see if in any point implements serializable or parceleable, otherwise transforming to a custom class will be needed.
you can use Google Gson to convert the object to a string and then pass it as a string.
from the receiving end convert the string to your original object.
Refer this ->
https://stackoverflow.com/a/33381385/9901309

Android can't serialize Kotlin lambda

we can read lambda function are serialize by default (https://discuss.kotlinlang.org/t/are-closures-serializable/1620),
but I getting error:
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.dialogfragment.Arguments)
my class Arguments:
class Arguments(val function: ()-> Unit) : Serializable
What is wrong with my lambda ?
(I got this error when Android need to kill my fragment because low of memory :) )
If you want to serialize or parcel something you need to ensure every field is serializable or parcelable.
In your case you are trying to serialize val function: ()-> Unit which is not serializable. Unfortunately, you can't make function serializable. You should rethink this argument class and pass something different. Probably you can get some parameter which leads to making a decision and then invoking a function in your fragment.

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.

Need for Parcelable in android

I want to know when to use parcelable and when not to . I know that parcelable is way of parcel complex data type in android , but as per official document http://developer.android.com/guide/faq/framework.html#3 , nothing such is mention . So when is parcelable really needed ??
By implementing the Parcelable interface, you can make your class object capable of being stored in a Bundle and you can then easily pass it to another activity with the help of an Intent.
e.g.
Intent i = new Intent(....);
i.putParcelableExtra("name", object);
Then in the other activity, get it like this:
YourClass object = (YourClass) getIntent().getExtras().getParcelableExtra("name");
Notice the typecasting. That is necessary in order to use your class's methods.
To pass data to another activity, we are usually put bundle object on activity intent that will be called. Bundle can be filled with primitive data types like long, integer, and boolean. It can be filled with simple data type like String class to represent text. For example, an activity calls another activity at the same time sends simple data to it.
In destination activity, we check the bundle. If it is exist, we open the data of bundle from the origin activity.
Now, how if we want to pass the complex data type like our defined class object to another activity? For this need, we can use Parcelable in Android.
Parcelable is an interface for classes so a class that implements Parcelable can be written to and read from a Parcel. The data in Parcel form can be passed between two threads. Parcel itself is a class that have abilities to serialize and deserialize object of class.

Categories

Resources