Skip to content

Commit

Permalink
改变传入 TargetClass 的方式
Browse files Browse the repository at this point in the history
  • Loading branch information
twiceyuan committed Jul 18, 2018
1 parent 946306f commit f7cbf14
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 28 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ data class Starter(
val parcelableBean: ParcelableBean, // 一个 Parcelable 类
val nestedBean: Father, // 一个 Serializable 嵌套 Serializable 类
val age: Int
) : ActivityArgs() {
// 定义要启动的目标 Activity
override fun targetClass() = ReceiverActivity::class.java
}
) : ActivityArgs(ReceiverActivity::class.java)
```


Expand Down Expand Up @@ -71,7 +68,7 @@ repositories {
dependencies {
// 添加引用
implementation 'com.twiceyuan:activityargs:1.2.0'
implementation 'com.twiceyuan:activityargs:1.2.1'
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
classpath 'com.android.tools.build:gradle:3.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ uploadArchives {
}
pom.groupId = "com.twiceyuan"
pom.artifactId = "activityargs"
pom.version = "1.2.0"
pom.version = "1.2.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ import kotlin.reflect.jvm.javaType
* }
* ```
*/
abstract class ActivityArgs {
abstract class ActivityArgs(@Transient private val targetClass: Class<out Activity>) {
fun launch(context: Context) {
context.startActivityWithArgs(targetClass(), this)
context.startActivityWithArgs(targetClass, this)
}

// 使用抽象方法而不是放入构造器或抽象属性的原因是:保持数据类成员属性的纯净性,避免影响 gson 等序列化工具不能直接进行序列化
abstract fun targetClass(): Class<out Activity>
}

/**
Expand Down
1 change: 0 additions & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'

implementation 'com.google.code.gson:gson:2.8.2'
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.twiceyuan.activityargs.sample

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.twiceyuan.activityargs.R
Expand All @@ -14,15 +15,28 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

btn_launch.setOnClickListener {
ReceiverActivity.Starter(
name = "Tony",
phone = "123456789",
emails = arrayListOf("somemail1@gmail.com, somemail2@gmail.com"),
age = 25,
parcelableBean = ParcelableBean("Tony", 100),
nestedBean = Father("Father", 25, Child("child", 1))
).launch(this)
val starter = ReceiverActivity.Starter(
name = "Tony",
phone = "123456789",
emails = arrayListOf("somemail1@gmail.com, somemail2@gmail.com"),
age = 25,
parcelableBean = ParcelableBean("Tony", 100),
nestedBean = Father("Father", 25, Child("child", 1))
)

btn_launch_by_starter.setOnClickListener {
starter.launch(this)
}

btn_launch_by_fun.setOnClickListener {
val intent = Intent(this, ReceiverActivity::class.java)
intent.putExtra("name", starter.name)
intent.putExtra("phone", starter.phone)
intent.putExtra("emails", starter.emails)
intent.putExtra("age", starter.age)
intent.putExtra("parcelableBean", starter.parcelableBean)
intent.putExtra("nestedBean", starter.nestedBean)
startActivity(intent)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ class ReceiverActivity : AppCompatActivity() {
val parcelableBean: ParcelableBean,
val nestedBean: Father,
val age: Int
) : ActivityArgs() {
override fun targetClass() = ReceiverActivity::class.java
}
) : ActivityArgs(ReceiverActivity::class.java)

private val args by lazy { parseActivityArgs<Starter>() }

Expand Down
11 changes: 9 additions & 2 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@
tools:context="com.twiceyuan.activityargs.sample.MainActivity">

<Button
android:id="@+id/btn_launch"
android:id="@+id/btn_launch_by_starter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/launch"/>
android:text="@string/launch_by_starter"/>

<Button
android:id="@+id/btn_launch_by_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/launch_by_fun"/>

</LinearLayout>
3 changes: 2 additions & 1 deletion sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<resources>
<string name="app_name">ActivityArgs Demo</string>
<string name="launch">启动新界面并传值</string>
<string name="launch_by_starter">使用 ActivityArgs 传值</string>
<string name="launch_by_fun">使用普通方式传值</string>
</resources>

0 comments on commit f7cbf14

Please sign in to comment.