What is a EasyActivityResult

EasyActivityResult is one of the base components in EasyAndroid, the open source base component integration library. The onActivityResult service logic is bloated

EasyAndroid as an integrated component library, this library of integrated components, including the following features, you can rest assured to use ~~

  1. Streamlining: As an integration library, I don’t want to have big components, and try to control the size of the integration library. Don’t have redundant code
  2. Cohesion: Minimize or even avoid the dependence of a single component on other modules. Make components independent.

If you just use EasyActivityResult, thanks to high cohesion when coding. So you can copy the EasyActivityResult source file directly to your project, direct use, is no problem.

A problem occurs during the Activity data sending service

In normal development, when we need to send data back to the page, we start with startActivityForResult, and then handle the respective business logic in the onActivityResult method. Such as picture taking, such as address selection.

This approach, however, is not elegant, and the onActivityResult method tends to get out of control and become bloated and difficult to maintain as the business continues to iterate, etc. A bunch of if-else if blocks appear.

Looking at the usage scenario for this data transfer service, the requirement should be the starting branch of each different startActivityForResult. Both correspond to a postback judgment branch of onActivityResult. There is a one-to-one correspondence.

In other words, if we can easily send the returned data to the corresponding startActivityForResult branch from onActivityResult, whoever initiated it will handle it by themselves. This business scenario is perfect.

The EasyActivityResult component is designed to do just that!

features

  1. To take overonActivityResultMethod to decouple data return logic
  2. In times of need,Automatically createdRequestCode provides usage without having to define it every timeDo not duplicate requestCodesteps
  3. Riot strike: The interval between two starts must be greater than 1 second.

usage

  1. First, in the base classonActivityResultAdd the relay method to the method:
class BaseActivity:Activity() { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {/ forward/back data filtering EasyActivityResult dispatch (this, requestCode, the resultCode, data)}}Copy the code
  1. Finally, use the easyActivityresult.start () method to start:
EasyActivityResult.startActivity( context, intent, {result:Int, Data :Intent -> // data callback interface // TODO // result: resultCode // data:Intent data})Copy the code

It’s that easy to use! And if you’re careful, you can see: ** no need to configure requestCode at all! ** saves another step. A fun ~ ~

The reason you don’t need to configure requestCode is this: inside the component. When you configure a data callback interface, then a random requestCode is automatically generated to provide use:

if(context ! is Activity || callback == null) { context.startActivity(intent) }else {
	val requestCode = codeGenerator.nextInt(0x0000FFFF)
	...
}
Copy the code

And since we also took over the boot portal. So it can also be very convenient for anti-riot filtering:

Take the following simulated critical hits as an example:

@OnClick(R.id.violentStart)
fun violentStart() {// Crit start test: Being called to start multiple times at the same time, Should be only the first time you start successful EasyActivityResult. StartActivity (this, Intent (this, EasyToastActivity: : class. Java), {_, _ -> EasyToast.DEFAULT.show("Critical launch test: First start task receive return message") })

    EasyActivityResult.startActivity(this,
            Intent(this, EasyToastActivity::class.java),
            {_, _ -> EasyToast.DEFAULT.show("Critical launch test: Second launch task receive return message") })

    EasyActivityResult.startActivity(this,
            Intent(this, EasyToastActivity::class.java),
            {_, _ -> EasyToast.DEFAULT.show("Critical launch test: receive return message for third launch task")})}Copy the code

Page effect display: