Giter VIP home page Giter VIP logo

mortar's Introduction

Mortar

介绍

今年年初启动的项目中计划使用JetPackNavigation框架,于是如何在Fragment之间传递数据就成为了一个新的问题。
因为Fragment中并没有像ActivitysetResultonActivityResult这样的API,而且Navigation对此也是毫无支持。
虽然在最新的Fragment-1.3.0-alpha08中有新加入的API,但是你看到alpha几个字符后,还是算了吧。
于是自己琢磨出了一套方法,相比官方推荐的一系列方法中,这套方法拥有以下优势:

  • 使用类型签名的方式进行匹配,不需要传递String类型的Key
  • 直接传递任意类型的对象,不需要实现SerializableParcelable接口
  • 无需声明接口或ViewModel、LiveData等

安装教程

1.在你的android工程的根目录下的build.gradle文件中适当的位置添加以下代码:

	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

2.在你的模块下的build.gradle文件中的适当位置添加以下代码:

	dependencies {
	        implementation 'com.gitee.numeron:mortar:0.1.0'
	}

初始化

1.在启用NavigationActivity中的onCreate方法中、setContentView之前,调用以下方法:

mortarEnabled()

使用方法

1.在要接收数据的AFragment中使用onResult方法:

onResult<List<User>> { resultCode: Int, list: List<User>? ->
    if(resultCode == RESULT_OK) {
    	if(!list.isNullOrEmpty()) {
            //TODO()
        }
    } else {
    	//TODO()
    }
}

2.在要传递数据的BFragment中使用setResult方法:

val userList: List<User> = ...
setResult(RESULT_OK, userList)

3.当从BFragment返回到AFragment时,之前通过onResult方法设置的Lambda回调就会运行。

  • 注:onResultsetResult传递的参数的类型必需一致,MutableList和List是不一样的,当其中一个为MutableList时,应该将类型声明为具体的List类型。

mortar's People

Contributors

xiazunyang avatar

Stargazers

 avatar  avatar

Watchers

 avatar

mortar's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.