Make writing a habit together! This is the 8th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge”. Click here for more details.

This article will implement the encapsulation of the elegant reading and writing of the Intent and Fragment Argument of an Activity from the Angle of attribute delegation

There are two ways to implement attribute delegation, which is directly implemented in the form of interface:

  • varAttribute delegate needs to be implementedReadWritePropertyinterface
  • valAttribute delegate needs to be implementedReadOnlyPropertyinterface

Since we only need to read the value, we simply implement the ReadOnlyProperty interface. The Intent delegate of the Activity reads the following code:

class IntentWrapper<T>(private valdefault: T) : ReadOnlyProperty<AppCompatActivity, T? > {override fun getValue(thisRef: AppCompatActivity, property: KProperty< * >): T? {
        return  when(default) {
            is Int -> thisRef.intent.getIntExtra(property.name, default)
            is String -> thisRef.intent.getStringExtra(property.name)
            else -> throw  Exception()
        } as? T
    }
}
Copy the code

Note that the default key used by the Intent to read the Activity is the property name :property.name, which means that the key must also use the property name when storing the value through the Intent.

If the key doesn’t want to use the property name for an Intent, then the property delegate class IntentWrapper needs to be modified to allow the constructor to pass in the key value from outside

IntentWrapper (String, Int, Boolean, Float, etc.) ¶

Take a look at the use:

private val data by IntentWrapper(56)

/ / read
printlin(data)
Copy the code

IntentWrapper is created manually each time and passed in a default value. We can wrap several commonly used types of methods to make it easier to implement:

fun intIntent(default: Int = 0) = IntentWrapper(default)

fun stringIntent(default: String = "") = IntentWrapper(default)
Copy the code

The intIntent() method gives a default value of 0, and the external can optionally pass in a default value, as well as any other type.

It can then be used like this:

private val data by intIntent()
Copy the code

The Fragment is an Intent that reads the Fragment’s Argument.

class ArgumentWrapper<T>(private valdefault: T) : ReadOnlyProperty<Fragment, T? > {override fun getValue(thisRef: Fragment, property: KProperty< * >): T? {
        return when(default) {
            is Int-> thisRef.arguments? .getInt(property.name, default)isString -> thisRef.arguments? .getString(property.name)else -> throw Exception()
        } as? T
    }
}
Copy the code

It also works like an Activity, so I won’t show it here. Of course, there are several common types of methods you can define to create ArgumentWrapper, just refer to the Activity handling above.

In the following article, I will consider the reading of encapsulated Activity’s Intent and Fragment’s Argument from the perspective of class delegation.