Navigation

223 阅读1分钟

SafeAgs

  • bundle: bundle是一个包含了各种存储数据类型(list, stack, array etc.)的结合,可以用来做各种数据类型间的转换。
  • 当我们想要从fragment A 传数据到 fragment B的时候,会遇到一个问题就是A的数据格式在转化的时候,可能会变成Null,0等,或者是说A的数据类型并不是B想要的,所以产生了safeArgs
  • safeArgs是gradle的一个插件,可以generate code 来保证两个fragment的数据类型能够匹配上,同时简化passing流程
  • Steps:
    1. 在project的builde.gradle添加dependencies

      classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:$navigationVersion"

    2. 在app的build.gradle(module)中, 添加plugin statement

      apply plugin: 'androidx.navigation.safeargs' 
      

Intent & Sharing(Activity之间的切换)

  • Explicit(明确的) intent: launch specific activity
  • Implicit intent: specifiy WHAT you want done and system chooses activity
  • Intent Action: 每个implicit intent都需要一个intent action:
    • describe the type of thing that is to be done
  • Intent Data Type: 通过数据类型来找到匹配的app
    • 所有的activity都需要在 androidManifest.xml regist
    • <intent-filter> is used to expose that your activity can response to an implicit intent with a certain action, category or type