MorbidMask(吸血面具)-安全快速的對Activity或者Fragment傳遞參數

MorbidMask - 吸血面具

一個安全快速的對Activity或者Fragment傳遞參數的庫git

物品介紹:github

攻擊者每次攻擊都將根據形成的傷害回覆15%的生命值。 吸血:15%express

Github地址

Start

First Blood

  • 給Activity添加註解:apache

    @Params( Val("intParam", Int::class),
        Val("booleanParam", Boolean::class), Val("stringParam", String::class)
    )
    open class TestActivity : AppCompatActivity() 
    複製代碼

    經過Params註解, 告訴該Activity須要的參數類型及參數名稱.安全

    例如TestActivity的參數分別爲:bash

    • intParam :Int類型
    • booleanParam :Boolean類型
    • stringParam :String參數
  • 傳遞參數給Activity:app

    Params註解編譯以後,將會生成該Activity對應的Director文件,命名規則爲 Activity名稱+Director後綴, 例如上面的TestActivity將會生成TestActivityDirector文件.less

    btn_activity.setOnClickListener {
        //經過Director安全快速傳遞參數給TestActivity
        TestActivityDirector.of(this)
            .intParam(1123123123)
            .booleanParam(true)
            .stringParam("This is string param")
            .direct()
    }
    複製代碼
  • 獲取傳遞給Activity的參數:ide

    Params註解編譯以後, 除了會生成Director文件, 還會生成對應的Params文件, 命名規則爲 Activity名稱+ Params後綴, 例如上面的TestActivity將會生成TestActivityParams文件.ui

    class TestActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_test)
    
            //獲取params參數
            val params = TestActivityParams.of(this)
          
            Log.d("TAG", params.intParam.toString())
            Log.d("TAG", params.booleanParam.toString())
            Log.d("TAG", params.stringParam)
        }
    }
    複製代碼

Double Kill

接下來嘗試給Fragment傳遞參數.

  • 添加Params註解:

    @Params( Val("intParam", Int::class),
        Val("booleanParam", Boolean::class), Val("stringParam", String::class)
    )
    class TestFragment : Fragment()
    複製代碼
  • 傳遞參數給Fragment:

    一樣的,Params註解編譯以後,將會生成該Fragment對應的Director文件,命名規則爲 Fragment名稱+Director後綴, 例如上面的TestFragment將會生成TestFragmentDirector文件.

    btn_fragment.setOnClickListener {
          TestFragmentDirector.of()
              .intParam(1123123123)
              .booleanParam(true)
              .stringParam("This is string param")
              .direct {
                  val fragmentTransaction = supportFragmentManager.beginTransaction()
                  fragmentTransaction.add(R.id.fragment_container, it)
                  fragmentTransaction.commit()
          }
    }
    複製代碼
  • 獲取傳遞給Fragment的參數:

    Params註解編譯以後, 除了會生成Director文件, 還會生成對應的Params文件, 命名規則爲 Fragment名稱+ Params後綴, 例如上面的TestFragment將會生成TestFragmentParams文件.

    class TestFragment : Fragment() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            val params = TestFragmentParams.of(this)
          
            Log.d("TAG", params.intParam.toString())
            Log.d("TAG", params.booleanParam.toString())
            Log.d("TAG", params.stringParam)
        }
    }
    複製代碼

Triple Kill

傳遞自定義數據類型

除了上述基本數據類型外,還支持自定義的數據類型.

  • 添加自定義類型參數:

    //自定義數據類型 
    class CustomEntity(
        val id: Int,
        val content: String
    )
    
    //使用自定義數據類型 
    @Params( Val("customParam", CustomEntity::class)
    )
    class TestActivity : AppCompatActivity() 
    複製代碼
  • 傳遞自定義類型參數:

    btn_activity.setOnClickListener {
        TestActivityDirector.of(this)
            .customParam(CustomEntity(123, "Custom entity content"))
            .direct()
    }
    複製代碼

自定義數據類型默認使用Gson進行序列化和反序列化, 所以請在release環境中Keep自定義的數據類型, 以避免發生問題!

Ultra Kill

利用MutableParams註解生成 var 類型參數.

如上所見, 經過Params註解只能定義val不可變類型的參數, 要想定義var可變類型的參數, 能夠經過 MutableParamsVar聲明可變類型的參數.

例如:

@Params( Val("charParam", Char::class),
    Val("booleanParam", Boolean::class), Val("stringParam", String::class)
)
@MutableParams( Var("test", String::class),
    Var("test1", Boolean::class)
)
class TestActivity : AppCompatActivity() 
複製代碼

License

Copyright 2019 Season.Zlc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
複製代碼
相關文章
相關標籤/搜索