Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Parcelable for Kotlin is not working

I am trying to pass data from one fragment to another but I am facing issue with sending data from parcelable from one fragment to another.

 class MainFragment : Fragment() {


    companion object {
        public val KEY_PARSE_DATA = "parseData"
    }

    private var parseData: ParseData? = null

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater!!.inflate(R.layout.fragment_main, container, false).apply {
            val editName = findViewById(R.id.edit_Name) as EditText
            val editSurname = findViewById(R.id.edit_Surname) as EditText
            val buttonNext = findViewById(R.id.btn_Next) as Button
            buttonNext.setOnClickListener(View.OnClickListener {
                val fragment = AnotherFragment()

                if (parseData != null) {
                    var parseData = ParseData(editName.text.toString(), editSurname.text.toString())
                    val fragment = AnotherFragment()
                    val bundle = Bundle()
                    bundle.putParcelable(KEY_PARSE_DATA, parseData)
                    fragment.setArguments(bundle)
                    fragmentManager.beginTransaction().add(R.id.fragment_container, fragment).commitAllowingStateLoss()

                }

            })
        }
    }


}// Required empty public constructor

Parcelable class for implementing it

 @SuppressLint("ParcelCreator")
@Parcelize
data class ParseData(val firstName: String, val lastName: String) : Parcelable {


    constructor(parcel: Parcel) : this(
            parcel.readString(),
            parcel.readString()) {
    }

    override fun toString(): String {
        return "ParseData(firstName='$firstName', lastName='$lastName')"
    }


    companion object : Parceler<ParseData> {

        override fun ParseData.write(parcel: Parcel, flags: Int) {
            parcel.writeString(firstName)
            parcel.writeString(lastName)
        }

        override fun create(parcel: Parcel): ParseData {
            return ParseData(parcel)
        }
    }
}

And another fragment which grab data from parcelable class in android

    class AnotherFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater!!.inflate(R.layout.fragment_another, container, false).apply {

            val textName = findViewById<TextView>(R.id.textFirst)
            val textSurname = findViewById<TextView>(R.id.textSecond)
            val bundle = arguments
            if (bundle != null) {
                val parseData = bundle.getParcelable<ParseData>(KEY_PARSE_DATA)
                textName.setText(parseData.firstName)
                textSurname.setText(parseData.lastName)
            }
        }
    }

}

I tried some example but I cant get clear idea how parcelable is implemented in andoid application based on kotlin and build.gradle file

 apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.assignment.ankitt.kotlinsecond"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

androidExtensions {
    experimental = true
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
like image 494
Ankit Tale Avatar asked Feb 28 '18 13:02

Ankit Tale


People also ask

How do I use kotlin Parcelable?

The first step is adding the kotlin-parcelize plugin to the shared module build. gralde file, till being able to use Parcelize annotation: As you know in regular Android projects if we want to make a class Parcelable, we should add the Parcelize annotation to it and implement the Parcelable interface.

What is Parcelize in kotlin?

Save. In Android, to pass the data from one activity to another activity, we use the Parcelable. The kotlin-parcelize plugin provides a Parcelable implementation generator. The Android's Parcelable is an interface on which, the values can be written and read from a Parcel.


2 Answers

Now it is enough to add 'kotlin-parcelize' plugin. So for example:

 plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-parcelize'

}

like image 184
Krzysiulele Avatar answered Oct 15 '22 16:10

Krzysiulele


Add below code into your App Level gradle.

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

androidExtensions {
    experimental = true
}

Check IMG

like image 26
Prashant Gosai Avatar answered Oct 15 '22 17:10

Prashant Gosai