Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

edited and updated versions #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions .gitignore

This file was deleted.

18 changes: 0 additions & 18 deletions .idea/gradle.xml

This file was deleted.

33 changes: 0 additions & 33 deletions .idea/misc.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/modules.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# KotlinParcelize

Please find the related article in the link below:

B
https://medium.com/@burakeregar/yet-another-awesome-kotlin-feature-parcelize-5439718ba220
1 change: 0 additions & 1 deletion app/.gitignore

This file was deleted.

21 changes: 10 additions & 11 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

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

android {
compileSdkVersion 26
compileSdkVersion 29
defaultConfig {
applicationId "com.burakeregar.kotlinparcelize"
minSdkVersion 14
targetSdkVersion 26
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
Expand All @@ -28,11 +27,11 @@ android {

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 "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
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'
compile 'org.jetbrains.anko:anko:0.10.3'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
implementation 'org.jetbrains.anko:anko:0.10.3'
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.burakeregar.kotlinparcelize

import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.burakeregar.kotlinparcelize

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("dataModel", PersonModel("Burak", 26))
startActivity(intent)
button.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("dataModel", PersonModel("Burak", 26))
startActivity(intent)
}
}
}
72 changes: 71 additions & 1 deletion app/src/main/java/com/burakeregar/kotlinparcelize/PersonModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,74 @@ import kotlinx.android.parcel.Parcelize
*/

@Parcelize
data class PersonModel(val name: String, val age: Int) : Parcelable
data class PersonModel(val name: String, val age: Int) : Parcelable


/*
Android Extensions plugin now includes an automatic Parcelable implementation generator.
Declare the serialized properties in a primary constructor and add a @Parcelize annotation,
and writeToParcel()/createFromParcel() methods will be created automatically.


How To Use:
Add the code below into your gradle.
androidExtensions {
experimental = true
}

You also need to add apply plugin: ‘kotlin-android-extensions’ to your gradle file.
However, if you have created your project using Kotlin support, it should be added automatically.
Then add @Parcelize annotation to your model. That’s it! You don’t need to write any parcel methods anymore!
*/

/*
* We can write equivalent PersonModel class in Java as the following:
*
public class Student implements Parcelable{
private String id;
private String name;
private String grade;

// Constructor
public Student(String id, String name, String grade){
this.id = id;
this.name = name;
this.grade = grade;
}
// Getter and setter methods
.........
.........

// Parcelling part
public Student(Parcel in){
String[] data = new String[3];

in.readStringArray(data);
// the order needs to be the same as in writeToParcel() method
this.id = data[0];
this.name = data[1];
this.grade = data[2];
}

@Оverride
public int describeContents(){
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] {this.id,
this.name,
this.grade});
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Student createFromParcel(Parcel in) {
return new Student(in);
}

public Student[] newArray(int size) {
return new Student[size];
}
};
}
* */
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.burakeregar.kotlinparcelize

import android.support.v7.app.AppCompatActivity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import org.jetbrains.anko.toast

Expand All @@ -10,8 +10,8 @@ class SecondActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)

val person = intent.extras.getParcelable<PersonModel>("dataModel")
val person = intent.extras?.getParcelable<PersonModel>("dataModel")

toast("Name: ${person.name} age: ${person.age}")
toast("Name: ${person?.name} age: ${person?.age}")
}
}
9 changes: 5 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.burakeregar.kotlinparcelize.MainActivity">

<TextView
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:text="Click me to send PersonModel instance to another activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_second.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.burakeregar.kotlinparcelize.SecondActivity">

</android.support.constraint.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.2.0'
ext.kotlin_version = '1.3.61'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:4.0.0-alpha08'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536m

# When configured, Gradle will run in incubating parallel mode.
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1-rc-1-all.zip
Loading